命名空间
变体
操作

std::collate

来自 cppreference.cn
< cpp‎ | locale
 
 
 
 
 
定义于头文件 <locale>
template< class CharT >
class collate;

std::collate封装了字符串的特定于区域设置的整理(比较)和哈希。这个切面被std::basic_regex使用,并且可以通过std::locale::operator()直接应用于所有期望字符串比较谓词的标准算法。

cpp/locale/locale/facetstd-collate-inheritance.svg

继承图

目录

[编辑] 特化

标准库保证提供以下特化(它们 要求由任何区域设置对象实现

定义于头文件 <locale>
std::collate<char> 实现字节字符串的词典式排序
std::collate<wchar_t> 实现宽字符串的词典式排序

[编辑] 嵌套类型

类型 定义
char_type CharT
string_type std::basic_string<CharT>

[编辑] 数据成员

成员 描述
std::locale::id id [静态] facet 的标识符

[编辑] 成员函数

构造一个新的collate切面
(公开成员函数)
销毁一个collate切面
(受保护成员函数)
调用do_compare
(公有成员函数) [编辑]
调用do_transform
(公有成员函数) [编辑]
调用do_hash
(公有成员函数) [编辑]

[编辑] 受保护成员函数

使用此 facet 的排序规则比较两个字符串
(虚保护成员函数) [编辑]
转换字符串,以便整理可以通过比较来替代
(虚保护成员函数) [编辑]
使用此切面的整理规则生成整数哈希值
(虚保护成员函数) [编辑]

[编辑] 示例

#include <algorithm>
#include <iostream>
#include <locale>
#include <string>
#include <vector>
 
int main()
{
    std::locale::global(std::locale("en_US.utf8"));
    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

[编辑] 另请参阅

使用此区域设置的 collate facet 对两个字符串进行字典比较
(std::locale的公有成员函数) [编辑]
表示命名区域设置的系统提供的std::collate
(类模板) [编辑]