lexicographical_compare (3C++std) - Tru64 UNIX
Standard C++ LibraryCopyright 1996, Rogue Wave Software, Inc. NAME
lexicographical_compare - Compares two ranges lexicographically.
SYNOPSIS
#include <algorithm>
template <class InputIterator1, class InputIterator2>
bool
lexicographical_compare(InputIterator1 first,
InputIterator2 last1,
InputIterator2 first2,
InputIterator last2);
template <class InputIterator1, class InputIterator2, class Compare>
bool
lexicographical_compare(InputIterator1 first,
InputIterator2 last1,
InputIterator2 first2,
InputIterator last2, Compare comp);
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;
}
Output:
FALSE TRUE
WARNING
If your compiler does not support default template parameters, then you
need to always supply the Allocator template argument. For instance,
you'll have to write :
vector<int, allocator<int> >
instead of :
vector<int>
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
|