Open In App

lexicographical_compare() in C++ STL

C++ STL offers many utilities to solve basic common life problems. Comparing values are always necessary, but sometimes we need to compare the strings also. Therefore, lexicographical_compare() is used to compare strings

It is commonly used in dictionaries to arrange words alphabetically; it entails comparing elements that have the same position in both ranges consecutively against each other until one element is not equal to the other. The lexicographical comparison is the consequence of comparing these first non-matching components. This function is defined in <algorithm> header. 



Time complexity= 2 * min(N1, N2) 

where N1 = std::distance(beg1, end1) and N2 = std::distance(beg2, end2).



It has the following 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)
{
  while (beg1!=end1)
  {
    if (beg2==end2 || *beg2<*beg1) return false;
    else if (*beg1<*beg2) return true;
    ++beg1; ++beg2;
  }
  return (beg2!=end2);
}

Parameters:

Return value: Returns a boolean true, if range1 is strictly lexicographically smaller than range2 else returns a false.

Example:




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

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)
{
  while (beg1!=end1)
  {
    if (beg2==end2 || *beg2<*beg1) return false;
    else if (*beg1<*beg2) return true;
    ++beg1; ++beg2;
  }
  return (beg2!=end2);
}

Parameters: 

Return value: Returns a Boolean true, if range1 is strictly lexicographically smaller than range2 else returns a false.

Example:




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

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

Application: Comparing strings can be generally used in dictionary, where we need to place words in lexicographical order. An example of this can be to find the word which occurs 1st in the dictionary among a given set of words.





Output
The smallest string is : abacus

Exception in lexicographical_compare(): It throws an exception if either an element comparison or an operation on an iterator throws. If the arguments are invalid, they cause undefined behavior.


Article Tags :
C++