命名空间
变体
操作

std::collate

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

std::collate 封装了区域设置特定的排序(比较)和字符串哈希。此 facet 被 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 [static] facet 的标识符

[编辑] 成员函数

构造一个新的 collate facet
(公共成员函数)
析构一个 collate facet
(受保护成员函数)
调用 do_compare
(公共成员函数) [编辑]
调用 do_transform
(公共成员函数) [编辑]
调用 do_hash
(公共成员函数) [编辑]

[编辑] 受保护成员函数

[虚函数]
使用此 facet 的排序规则比较两个字符串
(虚函数受保护成员函数) [编辑]
[虚函数]
转换字符串,以便排序可以被比较替换
(虚函数受保护成员函数) [编辑]
[虚函数]
使用此 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
(类模板) [编辑]