From Cppreference
|
|
|
|
|
template< class charT, class traits, class Allocator >
bool operator()( const basic_string<charT,traits,Allocator>& s1,
const basic_string<charT,traits,Allocator>& s2) const;
|
|
|
|
|
Compares two string arguments s1 and s2 according to the lexicographic comparison rules defined by this locale's std::collate<charT> facet. This operator allows any locale object that has a collate facet to be used as a binary predicate in the standard algorithms (such as std::sort) and ordered containers (such as std::set)
[edit] Parameters
s1
|
-
|
the first string to compare
|
s2
|
-
|
the second string to compare
|
[edit] Return value
true if s1 is lexicographically less than s2, false otherwise.
[edit] Equivalent function
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 std::use_facet<std::collate<charT>>(*this).compare(
s1.data(), s1.data() + s1.size(),
s2.data(), s2.data() + s2.size() ) < 0;
}
|
[edit] Example
A vector of strings can be sorted according to a non-default locale by using the locale object as comparator:
#include <locale>
#include <algorithm>
#include <vector>
#include <string>
#include <cassert>
int main()
{
std::vector<std::wstring> v = {L"жил", L"был", L"кот"};
std::sort(v.begin(), v.end(), std::locale("ru_RU.UTF8"));
assert(v[0] == L"был");
assert(v[1] == L"жил");
assert(v[2] == L"кот");
}
[edit] See also
|
|
defines lexicographical comparison and hashing of strings (class template)
|