c++11 标准模板(STL)本地化库 - 平面类别 (std::collate_byname)表示系统提供的具名本地环境的 std::collate

本地化库

本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。

平面类别

表示系统提供的具名本地环境的 std::collate

std::collate_byname

template< class CharT >
class collate_byname : public std::collate<CharT>;

std::collate_byname 是封装本地环境限定对照(比较)和字符串哈希的 std::collate 平面。正如同 std::collate ,它可以感染于 std::regex ,并以 std::locale::operator() 直接应用到所有期待 string 比较谓词的标准算法。

标准库提供二个特化

定义于头文件 <locale>

std::collate_byname<char>多字节字符串的本地环境限定对照
std::collate_byname<wchar_t>宽字符串的本地环境限定对照

成员函数

(构造函数)

构造新的 collate_byname 平面
(公开成员函数)

(析构函数)

析构 collate_byname 平面
(受保护成员函数)

std::collate_byname::collate_byname

explicit collate_byname( const char* name, std::size_t refs = 0 );

explicit collate_byname( const std::string& name, std::size_t refs = 0 );

(C++11 起)

为名为 name 的本地环境构造新的 std::collate_byname 平面。

refs 用于资源管理:在销毁最后一个保有平面的 std::locale 对象时,若 refs == 0 ,则实现销毁平面对象。否则,不销毁对象。

参数

name-本地环境的名称
refs-链接到该平面的引用数

std::collate_byname::~collate_byname

protected:
~collate_byname();

销毁平面。

继承自 std::collate

成员类型

成员类型定义
char_typecharT
string_typestd::basic_string<charT>

成员函数

compare

调用 do_compare
(std::collate<CharT> 的公开成员函数)

transform

调用 do_transform
(std::collate<CharT> 的公开成员函数)

hash

调用 do_hash
(std::collate<CharT> 的公开成员函数)

受保护成员函数

do_compare

[虚]

用此平面的对照规则比较二个字符串
(std::collate<CharT> 的虚受保护成员函数)

do_transform

[虚]

变换字符串,使得能用比较替换对照
(std::collate<CharT> 的虚受保护成员函数)

do_hash

[虚]

生成使用此平面对照规则的整数哈希值
(std::collate<CharT> 的虚受保护成员函数)

注意

对照顺序为字典顺序:国家字母表(其等价类)中字母的位置拥有高于其大小写或变体的优先级。在等价类内,小写字符先于其大写等价物对照,而且本地环境限定的顺序可能应用到有发音符号的字符。一些本地环境中,字符组作为单个对照单元比较。例如, "ch" 在捷克语中后随 "h" 而前趋 "i" , "dzs" 在匈牙利语中后随 "dz" 而前趋 "g" 。

调用示例 windows

#include <locale>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <Windows.h>
#include <unordered_set>

std::vector<std::wstring> locals;

BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
    locals.push_back(pStr);
    return TRUE;
}

std::string stows(const std::wstring& ws)
{
    std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
    setlocale(LC_ALL, "chs");
    const wchar_t* _Source = ws.c_str();
    size_t _Dsize = 2 * ws.size() + 1;
    char *_Dest = new char[_Dsize];
    memset(_Dest, 0, _Dsize);
    wcstombs(_Dest, _Source, _Dsize);
    std::string result = _Dest;
    delete[]_Dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}

struct CollateHash
{
    template<typename CharT>
    std::size_t operator()(const std::basic_string<CharT>& s) const
    {
        return std::use_facet<std::collate_byname<CharT>>(std::locale()).hash(
                   &s[0], &s[0] + s.size()
               );
    }
};

struct CollateEq
{
    template<typename CharT>
    bool operator()(const std::basic_string<CharT>& s1,
                    const std::basic_string<CharT>& s2) const
    {
        return std::use_facet<std::collate_byname<CharT>>(std::locale()).compare(
                   &s1[0], &s1[0] + s1.size(),
                   &s2[0], &s2[0] + s2.size()
               ) == 0;
    }
};

int main()
{
    EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALTERNATE_SORTS, NULL, NULL);

    for (std::vector<std::wstring>::const_iterator strIt = locals.begin();
            strIt != locals.end(); ++strIt)
    {
        std::locale::global(std::locale(stows(*strIt)));
        std::wcout.imbue(std::locale());
        std::wcout << "In the " << *strIt << " locale: ";

        std::unordered_set<std::wstring, CollateHash, CollateEq> unordered_set
            = {L"Foo", L"Bar", L"ABC"};

        for (auto& str : unordered_set)
        {
            std::wcout << str << ' ';
        }
        std::cout << std::endl;
    }

    return 0;
}

输出

In the de-DE_phoneb locale: Foo Bar ABC
In the es-ES_tradnl locale: Foo Bar ABC
In the hu-HU_technl locale: Foo Bar ABC
In the ja-JP_radstr locale: Foo Bar ABC
In the ka-GE_modern locale: Foo Bar ABC
In the x-IV_mathan locale: Foo Bar ABC
In the zh-CN_phoneb locale: Foo Bar ABC
In the zh-CN_stroke locale: Foo Bar ABC
In the zh-HK_radstr locale: Foo Bar ABC
In the zh-MO_radstr locale: Foo Bar ABC
In the zh-MO_stroke locale: Foo Bar ABC
In the zh-SG_phoneb locale: Foo Bar ABC
In the zh-SG_stroke locale: Foo Bar ABC
In the zh-TW_pronun locale: Foo Bar ABC
In the zh-TW_radstr locale: Foo Bar ABC

  • 25
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值