首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::collate

Defined in header <locale>

?

?

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 type

Definition

char_type

CharT

string_type

std::basic_string<CharT>

成员函数

(constructor)

constructs a new collate facet (public member function)

(destructor)

destructs a collate facet (protected member function)

compare

invokes do_compare (public member function)

transform

invokes do_transform (public member function)

hash

invokes do_hash (public member function)

成员对象

static std::locale::id id

id of the locale (public member object)

受保护成员函数

do_compare virtual

compares two strings using this facet's collation rules (virtual protected member function)

do_transform virtual

transforms a string so that collation can be replaced by comparison (virtual protected member function)

do_hash virtual

generates an integer hash value using this facet's collation rules (virtual protected member function)

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
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_byname

creates a collate facet for the named locale (class template)

代码语言:txt
复制
 ? cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com