lexicographical_compare in C++ - GeeksforGeeks

17 Aug, 2018
C++ STL offer many utilities to solve basic common life problems. Comparing values are always necessary, but sometimes we need to compare the strings also. Therefore, this article aims at explaining about “lexicographical_compare()” that allows to compare strings. This function is defined in “algorithm” header. It has two implementations.
Syntax 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<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";              }      }
Output:
geeksforgeeks is lexicographically less than gfg

Syntax 2 : lexicographical_compare(iter1 beg1, iter1 end1, iter2 beg2, iter2 end2, Compare 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<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 )";              }      }
Output:
geeksforgeeks is not lexicographically less than Gfg
geeksforgeeks is lexicographically less than Gfg( case-insensitive )

Possible application : Comparing strings can be generally used in dictionary, where we need to place words in lexicographical order. Example of this can be to find the word which occurs 1st in dictionary among given set of words.
// 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];    }            }
Output:
The smallest string is : abacus

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
cpp-algorithm-library
lexicographic-ordering
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Iterators in C++ STL
Operator Overloading in C++
Friend class and function in C++
Polymorphism in C++
Sorting a vector in C++
Convert string to char array in C++
Inline Functions in C++
List in C++ Standard Template Library (STL)
std::string class in C++
new and delete operators in C++ for dynamic memory