Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ lexicographical_compare(3C++) — Sun WorkShop 5.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought

lexicographical_compare(3C++)

Standard C++ Library
Copyright 1998, 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 included in 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 <functional>
#include <iostream>

using namespace std;

 

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;

}
 

Program Output
 
 
 

 
FALSE TRUE
 
 
 

WARNINGS

 
 
If your compiler does not support default template parameters, then you always need to supply the Allocator template argument. For instance, you have to write:
 
vector<int, allocator<int> >
 
instead of:
 
vector<int>
 
If your compiler does not support namespaces, then you do not need the using declaration for std. 
 

Rogue Wave Software  —  Last change: 02 Apr 1998

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026