📜  C++中的lexicographical_compare

📅  最后修改于: 2021-05-30 07:09:18             🧑  作者: Mango

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.
// C++ code to demonstrate the working of 
// lexicographical_compare()
  
#include
#include // 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";
          
    }
      
}

输出:

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.
// C++ code to demonstrate the working of 
// lexicographical_compare()
  
#include
#include // for lexicographical_compare()
using namespace std;
  
// helper function to convert all into lower case:
bool comp (char s1, char s2)
{
    return tolower(s1)

输出:

geeksforgeeks is not lexicographically less than Gfg
geeksforgeeks is lexicographically less than Gfg( case-insensitive )

可能的应用:比较字符串通常可以在字典中使用,在字典中我们需要按字典顺序放置单词。这样的示例可以是在给定的一组单词中找到在字典中排在第一位的单词。

// C++ code to demonstrate the application of 
// lexicographical_compare()
  
#include
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<

输出:

The smallest string is : abacus
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”