Standard C++ Library Class Reference
Description
The lexicographical_compare functions compare each element in the range [first1, last1) to the
corresponding element in the range [first2, last2) using iterators i and j.
The first version of the algorithm uses operator< as the default comparison operator. It immediately
returns true if it encounters any pair in which *i is less than *j, and immediately returns false if *j is
less than *i. If the algorithm reaches the end of the first sequence before reaching the end of the
second sequence, it also returns true.
The second version of the function takes an argument comp that defines a comparison function that is
used in place of the default operator<.
The lexicographical_compare functions can be used with all the datatypes provided by the standard
library.
Complexity
lexicographical_compare performs at most min((last1 - first1), (last2 - first2)) applications of the
comparison function.
Example
//
// lex_comp.cpp
//
#include <algorithm>
#include <vector>
#include <iostream.h>
int main(void)
{
int d1[5] = {1,3,5,32,64};
int d2[5] = {1,3,2,43,56};
// set up vector
vector<int> v1(d1,d1 + 5), v2(d2,d2 + 5);
// Is v1 less than v2 (I think not)
bool b1 = lexicographical_compare(v1.begin(),
v1.end(), v2.begin(), v2.end());
// Is v2 less than v1 (yup, sure is)
bool b2 = lexicographical_compare(v2.begin(),
v2.end(), v1.begin(), v1.end(), less<int>());
cout << (b1 ? "TRUE" : "FALSE") << " "
<< (b2 ? "TRUE" : "FALSE") << endl;
return 0;
}