C++ STL 中的 lexicographical_compare()
C++ STL 提供了许多实用程序来解决基本的常见生活问题。比较值总是必要的,但有时我们也需要比较字符串。因此, lexicographical_compare() 用于比较字符串。
它通常用于字典中按字母顺序排列单词;它需要连续比较两个范围内具有相同位置的元素,直到一个元素不等于另一个元素。字典比较是比较这些第一个不匹配的组件的结果。此函数在
它有以下两种实现:
语法 1:
lexicographical_compare(iter1 beg1, iter1 end1, iter2 beg2, iter2 end2)
模板:
template
bool lexicographical_compare(iter1 beg1, iter1 end1,
iter2 beg2, iter2 end2)
{
while (beg1!=end1)
{
if (beg2==end2 || *beg2<*beg1) return false;
else if (*beg1<*beg2) return true;
++beg1; ++beg2;
}
return (beg2!=end2);
}
参数:
- beg1:输入迭代器到第一个序列的初始位置。
- end1:输入迭代器到第一个序列的最终位置。
- beg2:输入迭代器到第二个序列的初始位置。
- end2:输入迭代器到第二个序列的最终位置。
返回值:返回布尔值 true,如果 range1 严格按字典顺序小于 range2,则返回 false。
例子:
CPP
// C++ code to demonstrate the working of
// lexicographical_compare(), implementation 1
#include
#include
using namespace std;
// Driver Code
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";
}
CPP
// C++ code to demonstrate the working of
// lexicographical_compare()
#include
#include
using namespace std;
// helper function to convert all into lower case:
bool comp(char s1, char s2)
{
return tolower(s1) < tolower(s2);
}
// Driver Code
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 )";
}
}
CPP
// C++ code to demonstrate the application of
// lexicographical_compare()
#include
using namespace std;
// Driver Code
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];
}
输出
geeksforgeeks is lexicographically less than gfg
语法 2:
lexicographical_compare(iter1 beg1, iter1 end1, iter2 beg2, iter2 end2, Compare comp)
模板:
template
bool lexicographical_compare(iter1 beg1, iter1 end1,
iter2 beg2, iter2 end2)
{
while (beg1!=end1)
{
if (beg2==end2 || *beg2<*beg1) return false;
else if (*beg1<*beg2) return true;
++beg1; ++beg2;
}
return (beg2!=end2);
}
参数:
- beg1:输入迭代器到第一个序列的初始位置。
- end1:输入迭代器到第一个序列的最终位置。
- beg2:输入迭代器到第二个序列的初始位置。
- end2:输入迭代器到第二个序列的最终位置。
- comp:比较器函数,返回每个比较元素的布尔值 true/false。该函数接受两个参数。这可以是函数指针或函数对象,并且不能更改值。
返回值:如果 range1 严格按字典顺序小于 range2,则返回布尔值 true,否则返回 false。
例子:
CPP
// C++ code to demonstrate the working of
// lexicographical_compare()
#include
#include
using namespace std;
// helper function to convert all into lower case:
bool comp(char s1, char s2)
{
return tolower(s1) < tolower(s2);
}
// Driver Code
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 )";
}
}
输出
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
using namespace std;
// Driver Code
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];
}
输出
The smallest string is : abacus
lexicographical_compare() 中的异常:如果元素比较或迭代器上的操作抛出,则会抛出异常。如果参数无效,它们会导致未定义的行为。