lexicographical_compare() in C++ STL
Last Updated :
09 Jan, 2023
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:
- beg1: Input iterator to the initial position of the first sequence.
- end1: Input iterator to the final position of the first sequence.
- beg2: Input iterator to initial position of the second sequence.
- end2: Input iterator to the final position of the second sequence.
Return value: Returns a boolean true, if range1 is strictly lexicographically smaller than range2 else returns a false.
Example:
CPP
// 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";
}
Outputgeeksforgeeks 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:
- beg1: Input iterator to the initial position of the first sequence.
- end1: Input iterator to the final position of the first sequence.
- beg2: Input iterator to initial position of the second sequence.
- end2: Input iterator to the final position of the second sequence.
- comp: The comparator function that returns a Boolean true/false of each element compared. This function accepts two arguments. This can be a 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.
Example:
CPP
// 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 )";
}
}
Outputgeeksforgeeks 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.
CPP
// C++ code to demonstrate the application of
// lexicographical_compare()
#include <bits/stdc++.h>
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];
}
OutputThe 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.
Similar Reads
lexicographical_compare in C++
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 "algori
4 min read
Compare two strings lexicographically in Java
In this article, we will discuss how we can compare two strings lexicographically in Java. One solution is to use Java compareTo() method. The method compareTo() is used for comparing two strings lexicographically in Java. Each character of both the strings is converted into a Unicode value for comp
3 min read
multimap key_comp in C++ STL
This is the part of Standard Template Library(STL) of C++. To use this STL, use Namespace: std and include âmapâ header file in the program. It returns the function object or comparison object or ordering delegate that compares the keys, which is a copy of this container's constructor argument. It i
2 min read
multimap key_comp() in C++ STL
The std::multimap::key_comp() is an inbuilt function in C++ STL which returns a copy of the comparison object used by the container.By default, this is a less object, which returns the same as operator â<'.It is a function pointer or a function object which takes two arguments of the same ty
2 min read
iswgraph() in C/C++ with Examples
The iswgraph() is a built-in function in C/C++ which checks if the given wide character has a graphical representation or not. It is defined within the cwctype header file of C++. By default, the following characters are graphic: Digits (0 to 9)Uppercase letters (A to Z)Lowercase letters (a to z)Pun
2 min read
iswalpha() function in C++ STL
The iswalpha() is a built-in function in C++ STL which checks if the given wide character is an alphabet or not. It is defined within the cwctype header file of C++. The following characters are alphanumeric: Uppercase letters: A to Z Lowercase letters: a to z Syntax: int iswalpha(ch) Parameter: The
2 min read
std::string::compare() in C++
The string::compare() function in C++ is used to compare a string or the part of string with another string or substring. It is the member function of std::string class defined inside <string> header file. In this article, we will learn how to use string::compare() in C++.The different ways to
4 min read
Ratio Manipulations in C++ | Set 2 (Comparison)
Prerequisite - Ratio Manipulations in C++ | Set 1(Arithmetic)In C++, the <ratio> header file allows us to manipulate ratios using various inbuilt template alias. The header file was introduced from C++11 onwards. In this article, we will be discussing the Comparison of Ratio Manipulations in C
4 min read
map value_comp() in C++ STL
The std::map::value_comp() is a function in C++ STL. It returns a function object that compares objects of type std::map::value. Syntax: value_compare value_comp() const Parameters: It does not accept any parameters. Returns: This method returns a function object that compares objects of type std::m
2 min read
set::key_comp() in C++ STL
set::key_comp() is an inbuilt function in C++ STL which returns a copy of the comparison object used by the container. By default, this is a less object, which returns the same as operator '. This object determines the order of the elements in the container. It is a function pointer or a function ob
2 min read