Skip to content. Skip to navigation

ICTP Portal

Sections
You are here: Home Manuals on-line PGI Compiler pgC_lib lexicographical_compare
Personal tools
Document Actions

lexicographical_compare



Click on the banner to return to the class reference home page.

lexicographical_compare


Algorithm

Summary

Compares two ranges lexicographically.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

None

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>


©Copyright 1996, Rogue Wave Software, Inc.


Powered by Plone This site conforms to the following standards: