Namespaces
Variants

std:: collate

From cppreference.net
定义于头文件 <locale>
template < class CharT >
class collate ;

std::collate 封装了区域特定的字符串排序(比较)和哈希处理。该facet被 std::basic_regex 使用,并可通过 std::locale::operator() 直接应用于所有需要字符串比较谓词的标准算法。

cpp/locale/locale/facet std-collate-inheritance.svg

继承关系图

目录

特化

标准库保证提供以下特化(它们 是任何locale对象都必须实现的 ):

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

嵌套类型

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

数据成员

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

成员函数

构造新的 collate facet
(公开成员函数)
销毁 collate facet
(受保护成员函数)
调用 do_compare
(公开成员函数)
调用 do_transform
(公开成员函数)
调用 do_hash
(公开成员函数)

受保护的成员函数

[virtual]
使用此facet的排序规则比较两个字符串
(虚函数保护成员函数)
[virtual]
转换字符串以便用比较替代排序
(虚函数保护成员函数)
[virtual]
使用此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

参见

使用此locale的collate facet按字典序比较两个字符串
( std::locale 的公开成员函数)
表示指定locale的系统提供 std::collate
(类模板)