I l@ve RuBoard Previous Section Next Section

lexicographical_compare()

By default, the less-than operator is applied, although an optional fifth option allows us to provide an alternative ordering operation. Returns true if the first sequence is less than or equal to the second sequence.



#include <algorithm> 


class size_compare { 


public: 


   bool operator()( const string &a, const string &b ) { 


        return a.length() <= b.length(); 


   } 


}; 


string sa1[] = { "Piglet", "Pooh",  "Tigger" }; 


string sa2[] = { "Piglet", "Pooch", "Eeyore" }; 





// false: 'c' less than 'h' 


res = lexicographical_compare( sa1, sa1+3, sa2, sa2+3 ); 





list<string> ilist1( sa1, sa1+3 ); 


list<string> ilist2( sa2, sa2+3 ); 





// true: Pooh < Pooch 


res = lexicographical_compare( 


         ilist1.begin(), ilist1.end(), 


         ilist2.begin(), ilist2.end(), size_compare() ); 
    I l@ve RuBoard Previous Section Next Section