C++ STL 提供了许多实用程序来解决基本的常见生活问题。比较值总是必要的,但有时我们也需要比较字符串。因此,本文旨在解释允许比较字符串的“lexicographical_compare()”。该函数在“algorithm”标头中定义。它有两个实现。语法1:lexicographical_compare(iter1 beg1, iter1 end1, iter2 beg2, iter2 end2)
Template: template bool lexicographical_compare(iter1 beg1, iter1 end1, iter2 beg2, iter2 end2) Parameters : beg1 : Input iterator to initial position of first sequence. end1 : Input iterator to final position of first sequence. beg2 : Input iterator to initial position of second sequence. end2 : Input iterator to final position of second sequence. Return value : Returns a boolean true, if range1 is strictly lexicographically smaller than range2 else returns a false.
CPP
// C++ code to demonstrate the working of
// lexicographical_compare()
#include<iostream>
#include<algorithm> // for lexicographical_compare()
using namespace std;
int main()
{
// initializing char arrays
char one[] = "geeksforgeeks";
char two[] = "gfg";
// using lexicographical_compare for checking
// is "one" is less than "two"
if( lexicographical_compare(one, one+13, two, two+3))
{
cout << "geeksforgeeks is lexicographically less than gfg";
}
else
{
cout << "geeksforgeeks is not lexicographically less than gfg";
}
}
时间复杂度:O(N)
空间复杂度:O(1)
输出:
geeksforgeeks is lexicographically less than gfg
语法 2: lexicographical_compare(iter1 beg1, iter1 end1, iter2 beg2, iter2 end2, 比较 comp)
Template: template bool lexicographical_compare(iter1 beg1, iter1 end1, iter2 beg2, iter2 end2) Parameters : beg1 : Input iterator to initial position of first sequence. end1 : Input iterator to final position of first sequence. beg2 : Input iterator to initial position of second sequence. end2 : Input iterator to final position of second sequence. comp : The comparator function that returns a boolean true/false of the each elements compared. This function accepts two arguments. This can be function pointer or function object and cannot change values. Return value : Returns a boolean true, if range1 is strictly lexicographically smaller than range2 else returns a false.
CPP
// C++ code to demonstrate the working of
// lexicographical_compare()
#include<iostream>
#include<algorithm> // for lexicographical_compare()
using namespace std;
// helper function to convert all into lower case:
bool comp (char s1, char s2)
{
return tolower(s1)<tolower(s2);
}
int main()
{
// initializing char arrays
char one[] = "geeksforgeeks";
char two[] = "Gfg";
// using lexicographical_compare for checking
// is "one" is less than "two"
// returns false as "g" has larger ASCII value than "G"
if( lexicographical_compare(one, one+13, two, two+3))
{
cout << "geeksforgeeks is lexicographically less than Gfg\n";
}
else
{
cout << "geeksforgeeks is not lexicographically less than Gfg\n";
}
// using lexicographical_compare for checking
// is "one" is less than "two"
// returns true this time as all converted into lowercase
if( lexicographical_compare(one, one+13, two, two+3, comp))
{
cout << "geeksforgeeks is lexicographically less ";
cout << "than Gfg( case-insensitive )";
}
else
{
cout << "geeksforgeeks is not lexicographically less ";
cout<< "than Gfg( case-insensitive )";
}
}
时间复杂度:O(N)
空间复杂度:O(1)
输出:
geeksforgeeks is not lexicographically less than Gfg geeksforgeeks is lexicographically less than Gfg( case-insensitive )
可能的应用:比较字符串一般可以用在字典中,我们需要按照字典顺序放置单词。例如,在给定的单词集中查找字典中第一个出现的单词。
CPP
// C++ code to demonstrate the application of
// lexicographical_compare()
#include<bits/stdc++.h>
using namespace std;
int main()
{
// initializing char arrays
char list[][100]={
{'a','b','a','c','u','s'},
{'a','p','p','l','e'},
{'c','a','r'},
{'a','b','b','a'}
};
char min[100] = "zzzzzz";
// using lexicographical_compare for checking
// the smallest
for (int i=0; i<4; i++)
{
if( lexicographical_compare(list[i], list[i]
+ strlen(list[i]), min, min+strlen(min)))
{
strcpy(min,list[i]);
}
}
// prints "abacus"
cout << "The smallest string is : ";
for(int i = 0; min[i]!='\0'; i++)
{
cout<<min[i];
}
}
时间复杂度:O(N)
空间复杂度:O(1)
输出:
The smallest string is : abacus
相关用法
- C++ lexicographical_compare()用法及代码示例
- C++ log()用法及代码示例
- C++ lround()用法及代码示例
- C++ llround()用法及代码示例
- C++ lrint()用法及代码示例
- C++ log10()用法及代码示例
- C++ ldexp()用法及代码示例
- C++ log2()用法及代码示例
- C++ log1p()用法及代码示例
- C++ logb()用法及代码示例
- C++ llrint()用法及代码示例
- C++ labs()用法及代码示例
- C++ ldiv()用法及代码示例
- C++ llabs()用法及代码示例
- C++ lldiv()用法及代码示例
- C++ localeconv()用法及代码示例
- C++ longjmp() and setjmp()用法及代码示例
- C++ localtime()用法及代码示例
- C++ list assign()用法及代码示例
- C++ list back()用法及代码示例
- C++ list emplace()用法及代码示例
- C++ list empty()用法及代码示例
- C++ list end()用法及代码示例
- C++ list erase()用法及代码示例
- C++ list front()用法及代码示例
注:本文由纯净天空筛选整理自佚名大神的英文原创作品 lexicographical_compare in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。