std::locale::operator()
template< class CharT, class Traits, class Alloc >
bool operator()( const basic_string<CharT,Traits,Alloc>& s1, |
||
按照此本地环境的 std::collate<charT> 刻面所定义的字典序比较规则,比较两个字符串实参 s1 与 s2。此运算符允许任何拥有校排平面的本地环境对象被用作标准算法(如 std::sort)和有序容器(std::set)中的二元谓词。
参数
s1 | - | 要比较的第一字符串 |
s2 | - | 要比较的第二字符串 |
返回值
若 s1 按字典序小于 s2 则为 true,否则为 false。
可能的实现
template<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; } |
示例
string 的 vector 能以本地环境对象为比较器,按照非默认的本地环境排序:
#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"кот"); }
参阅
定义字典序比较和字符串的散列 (类模板) |