Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
STL Algorithms
algorithm:
adjacent_find
binary_search
copy
copy_backward
count
count_if
equal
equal_range
fill
fill_n
find
find_end
find_first_of
find_if
for_each
generate
generate_n
includes
inplace_merge
iter_swap
lexicographical_compare
lower_bound
make_heap
max
max_element
merge
min
min_element
mismatch
next_permutation
nth_element
partial_sort
partial_sort_copy
partition
pop_heap
prev_permutation
push_heap
random_shuffle
remove
remove_copy
remove_copy_if
remove_if
replace
replace_copy
replace_copy_if
replace_if
reverse
reverse_copy
rotate
rotate_copy
search
search_n
set_difference
set_intersection
set_symmetric_difference
set_union
sort
sort_heap
stable_partition
stable_sort
swap
swap_ranges
transform
unique
unique_copy
upper_bound


lexicographical_compare

function template
<algorithm>
template <class InputIterator1, class InputIterator2>
  bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,
                                 InputIterator2 first2, InputIterator2 last2 );

template <class InputIterator1, class InputIterator2, class Compare>
  bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,
                                 InputIterator2 first2, InputIterator2 last2,
                                 Compare comp );

Lexicographical less-than comparison

Returns true if range [first1,last1) compares lexicographically less than the range [first2,last2).

Lexicographical comparison is used for example to sort words alphabetically in dictionaries (or in the list of algorithms right here on the left); It involves comparing sequentially the elements that have the same position in both ranges against each other until one element is not equivalent to the other. The result of comparing these first non-matching elements is the result of the lexicographical comparison.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
template <class InputIterator1, class InputIterator2>
  bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,
                                 InputIterator2 first2, InputIterator2 last2 )
{
  while (first1!=last1)
  {
    if (first2==last2 || *first2<*first1) return false;
    else if (*first1<*first2) return true;
    first1++; first2++;
  }
  return (first2!=last2);
}


Lexicographical comparisons of equivalence with a strict weak ordering criterion between two ranges can be achieved by negating two reflexive less-than comparisons: (a==b) implies (!a<b && !a>b).

Parameters

first1, last1
Input iterators to the initial and final positions of the first sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.
first2, last2
Input iterators to the initial and final positions of the second sequence. The range used is [first2,last2).
comp
Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument is to be considered less than the second argument.

Return value

true if the first range compares lexicographically less than than the second.
false if either both ranges are entirely equivalent or if is the second that compares less than the first.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// lexicographical_compare example
#include <iostream>
#include <algorithm>
#include <cctype>
using namespace std;
// a case-insensitive comparison function:
bool mycomp (char c1, char c2)
{ return tolower(c1)<tolower(c2); }
int main () {
  char first[]="Apple";         // 5 letters
  char second[]="apartment";    // 9 letters
  cout << "Using default comparison (operator<): ";
  if (lexicographical_compare(first,first+5,second,second+9))
    cout << first << " is less than " << second << endl;
  else
    if (lexicographical_compare(second,second+9,first,first+5))
      cout << first << " is greater than " << second << endl;
  else
    cout << first << " and " << second << " are equivalent\n";
  cout << "Using mycomp as comparison object: ";
  if (lexicographical_compare(first,first+5,second,second+9,mycomp))
    cout << first << " is less than " << second << endl;
  else
    if (lexicographical_compare(second,second+9,first,first+5,mycomp))
      cout << first << " is greater than " << second << endl;
  else
    cout << first << " and " << second << " are equivalent\n";
  return 0;
}


The default comparison compares plain ASCII character codes, where 'A' (65) compares less than 'a' (97).
Our mycomp function transforms the letters to lowercase before comparing them, so here the first letter not matching is the third ('a' vs 'p').

Output:
Using default comparison (operator<): Apple is less than apartment
Using mycomp as comparison object: Apple is greater than apartment

Complexity

At most, performs 2*min(count1,count2) comparisons or applications of comp (where countX is the distance between firstX and lastX).

See also