bool operator()( const std::basic_string<CharT,Traits,Alloc>& s1,
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 valuetrue if s1 is lexicographically less than s2, false otherwise.
[edit] Possible implementationtemplate<class CharT, class Traits, class Alloc> bool operator()(const std::basic_string<CharT,Traits,Alloc>& s1, const std::basic_string<CharT,Traits,Alloc>& 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 <algorithm> #include <cassert> #include <locale> #include <string> #include <vector> 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4