public member function
<locale>

std::locale::operator()

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 s1 to s2 according to the ordering rules defined by the collate facet in the locale, 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
// locale::operator() example
#include <iostream>       // std::cout, std::boolalpha
#include <locale>         // std::locale
#include <string>         // std::string
#include <algorithm>      // std::sort

int main (void) {
  std::string mystr[]= {"light","zoo","apple"};
  std::locale loc;  // default global locale

  std::cout << std::boolalpha;

  // implicit call to locale::operator() (using operator)
  std::cout << mystr[0] << " < " << mystr[1] << " ? ";
  std::cout << ( loc(mystr[0],mystr[1]) ) << '\n';

  // explicit call to locale::operator() (using functional call)
  std::cout << mystr[1] << " < " << mystr[2] << " ? ";
  std::cout << ( loc.operator()(mystr[1],mystr[2]) ) << '\n';

  // locale::operator() as comparison predicate for algorithm:
  std::sort (mystr,mystr+3,loc);
  std::cout << "sorted sequence:";
  for (int i=0; i<3; i++) std::cout << ' ' << mystr[i];
  std::cout << '\n';
  return 0;
}

Possible output:

light < zoo ? true
zoo < apple ? false
sorted sequence: apple light zoo


Data races

The locale object and the function arguments are accessed.

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in any object.

See also