public member function
std::locale::operator()
<locale>
template <class charT, class Traits, class Allocator>
bool operator() (const basic_string<charT,Traits,Allocator>& s1,
const basic_string<charT,Traits,Allocator>& s2) const;
Compare strings using locale
Compares string
s1 to string
s2 according to the ordering rules defined by the
collate facet, and returns whether
s1 goes before
s2 in the collation order.
Its definition behaves like:
1 2 3 4 5 6 7
|
template <class charT, class Traits, class Allocator>
bool operator() (const basic_string<charT,Traits,Allocator>& s1,
const basic_string<charT,Traits,Allocator>& s2) const
{
return ( use_facet< collate<charT> > (*this).compare
(s1.data(), s1.data()+s1.size(), s2.data(), s2.data()+s2.size()) < 0 );
}
|
This member function of an object can be used as a comparison predicate for standard
algorithms. Therefore, any locale object is itself also a valid predicate (see example below).
Parameters
- s1, s2
- basic_string objects to be compared in a locale sensitive manner.
The template parameters are used for the basic_string type.
Return value
true if
s1 goes before
s2 in the specific
strict weak ordering the
collate facet defines for strings, and
false otherwise (including, but not limited to, when
s1 is equal to
s2).
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
|
// locale::operator() example
#include <iostream>
#include <locale>
#include <string>
#include <algorithm>
using namespace std;
int main (void) {
string mystr[]= {"light","zoo","apple"};
locale loc; // default global locale
cout << boolalpha;
// implicit call to locale::operator() (using operator)
cout << mystr[0] << " < " << mystr[1] << " ? ";
cout << ( loc(mystr[0],mystr[1]) ) << endl;
// explicit call to locale::operator() (using function call)
cout << mystr[1] << " < " << mystr[2] << " ? ";
cout << ( loc.operator()(mystr[1],mystr[2]) ) << endl;
// locale::operator() as comparison predicate for algorithm:
sort (mystr,mystr+3,loc);
cout << "sorted sequence: ";
for (int i=0; i<3; i++) cout << mystr[i] << " ";
cout << endl;
return 0;
}
|
Possible output:
light < zoo ? true
zoo < apple ? false
sorted sequence: apple light zoo
|