在线文档教程
C++
本土化 | Localizations

std::collate

STD:校对

Defined in header
template< class CharT > class collate;

std::collate封装特定于地区的排序规则%28比较%29和字符串散列.。此面由std::basic_regex并且可以通过std::locale::operator(),直接指向所有期望字符串比较谓词的标准算法。

二次

二次

继承图

标准库提供了两个独立的%28区域设置无关%29的专门化:

在标头中定义<locale>

*。

STD:校对<char>实现字节字符串的字典排序

STD::整理<wchar[医]实现宽字符串的字典排序

此外,在C++程序中构造的每个locale对象都实现了自己的%28 locale特定于这些专门化的%29版本。

成员类型

Member typeDefinition
char_typeCharT
string_typestd::basic_string<CharT>

成员函数

(constructor)constructs a new collate facet (public member function)
(destructor)destructs a collate facet (protected member function)
compareinvokes do_compare (public member function)
transforminvokes do_transform (public member function)
hashinvokes do_hash (public member function)

成员对象

static std::locale::id idid of the locale (public member object)

受保护成员函数

do_compare virtualcompares two strings using this facet's collation rules (virtual protected member function)
do_transform virtualtransforms a string so that collation can be replaced by comparison (virtual protected member function)
do_hash virtualgenerates an integer hash value using this facet's collation rules (virtual protected member function)

二次

#include <locale> #include <iostream> #include <string> #include <vector> #include <algorithm> int main() { std::wcout.imbue(std::locale("") std::vector<std::wstring> v = {L"ar", L"zebra", L"\u00f6grupp", L"Zebra", L"\u00e4ngel", L"\u00e5r", L"f\u00f6rnamn"}; std::wcout << "Default locale collation order: "; std::sort(v.begin(), v.end() for (auto s : v) std::wcout << s << ' '; std::wcout << '\n'; std::wcout << "English locale collation order: "; std::sort(v.begin(), v.end(), std::locale("en_US.UTF-8") for (auto s : v) std::wcout << s << ' '; std::wcout << '\n'; std::wcout << "Swedish locale collation order: "; std::sort(v.begin(), v.end(), std::locale("sv_SE.UTF-8") for (auto s : v) std::wcout << s << ' '; std::wcout << '\n'; }

二次

产出:

二次

Default locale collation order: Zebra ar förnamn zebra ängel år ögrupp English locale collation order: ängel ar år förnamn ögrupp zebra Zebra Swedish locale collation order: ar förnamn zebra Zebra år ängel ögrupp

二次

另见

operator()lexicographically compares two strings using this locale's collate facet (public member function of std::locale)
collate_bynamecreates a collate facet for the named locale (class template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/locale/COLATE