c++11 标准模板(STL)本地化库 - 平面类别(std::collate) - 定义字典序比较和字符串的散列(二)

本地化库

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

平面类别

定义字典序比较和字符串的散列

std::collate

std::collate 封装字符串的本地环境限定对照(比较)和哈希。此平面为 std::basic_regex 所用,并能以 std::locale::operator() 直接应用到所有期待 string 比较谓词的标准算法。

继承图

标准库提供二个孤立(不依赖本地环境)的特化:

定义于头文件 <locale>

std::collate<char>实现字节字符串的字典序定序
std::collate<wchar_t>实现宽字符串的字典序定序

另外, C++ 程序中构造的每个 locale 对象都实装这些特化的其自身(本地环境限定)版本。

成员类型

成员类型定义
char_typeCharT
string_typestd::basic_string<CharT>

 

构造新的 collate 平面

std::collate<CharT>::collate

explicit collate( std::size_t refs = 0 );

创建 std::collate 平面并转发引用计数 refs 到基类构造函数 locale::facet::facet() 。

参数

refs-开始的引用计数

销毁 collate 平面

std::collate<CharT>::~collate

protected: ~collate();

析构 std::collate 平面。此析构函数为受保护且为虚(由于基类析构函数为虚)。 std::collate 类型对象,同大多数平面,只能在最后一个实装此平面的 std::locale 离开作用域时,或若用户定义导出自 std::collate 并实现公开构造函数,才会被销毁。

示例

#include <iostream>
#include <locale>

struct Destructible_collate : public std::collate<wchar_t>
{
    Destructible_collate(std::size_t refs = 0) : collate(refs) {}
    // 注意:隐式析构函数为公开
};

int main()
{
    Destructible_collate dc;
    // std::collate<wchar_t> c;  // 编译错误:受保护析构函数

    return 0;
}

调用 do_compare & 用此平面的对照规则比较二个字符串

std::collate<CharT>::compare, 
std::collate<CharT>::do_compare
public:

int compare( const CharT* low1, const CharT* high1,

             const CharT* low2, const CharT* high2 ) const;
(1)
protected:

virtual int do_compare( const CharT* low1, const CharT* high1,

                        const CharT* low2, const CharT* high2 ) const;
(2)

1) 公开成员函数,调用最终导出类的受保护虚成员函数 do_compare

2) 以此本地环境的对照规则,比较字符序列 [low1, high1) 与字符序列 [low2, high2) ,而若第一字符串后随第二个则返回 1 ,若第一字符串前趋第二个则返回 -1 ,若二个字符串等价则返回零。

参数

low1-指向第一字符串首字符的指针
high1-第一字符串的尾后一位置指针
low2-指向第二字符串首字符的指针
high2-第二字符串的尾后一位置指针

返回值

若第一字符串大于第二个(即以对照顺序后随第二个)则为 1 ,若第一字符串小于第二个(以对照顺序前趋第二个)则为 -1 ,若二个字符串等价则为零。

注意

不要求三路比较时(例如在提供 Compare 参数给如 std::sort 的标准算法时), std::locale::operator() 可能更适合。

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

 

调用示例 linux

#include <iostream>
#include <string>
#include <locale>

template<typename CharT>
void try_compare(const std::locale& l, const CharT* p1, const CharT* p2)
{
    auto& f = std::use_facet<std::collate<CharT>>(l);

    std::basic_string<CharT> s1(p1), s2(p2);
    if (f.compare(&s1[0], &s1[0] + s1.size(),
                  &s2[0], &s2[0] + s2.size()) < 0)
    {
        std::wcout << p1 << " before " << p2 << '\n';
    }
    else
    {
        std::wcout << p2 << " before " << p1 << '\n';
    }
}

int main()
{
    std::locale::global(std::locale("en_US.utf8"));
    std::wcout.imbue(std::locale());

    std::wcout << "In the American locale: ";
    try_compare(std::locale(), "hrnec", "chrt");
    std::wcout << "In the Czech locale: ";
    try_compare(std::locale("cs_CZ.utf8"), "hrnec", "chrt");

    std::wcout << "In the American locale: ";
    try_compare(std::locale(), L"år", L"ängel");
    std::wcout << "In the Swedish locale: ";
    try_compare(std::locale("sv_SE.utf8"), L"år", L"ängel");

    return 0;
}

输出

In American locale: chrt before hrnec
In the Czech locale: hrnec before chrt
In the American locale: ängel before år
In the Swedish locale: år before ängel

调用示例 window

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

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;
}

template<typename CharT>
void try_compare(const std::locale& l, const CharT* p1, const CharT* p2)
{
    auto& f = std::use_facet<std::collate<CharT>>(l);

    std::basic_string<CharT> s1(p1), s2(p2);
    if (f.compare(&s1[0], &s1[0] + s1.size(),
                  &s2[0], &s2[0] + s2.size()) < 0)
    {
        std::wcout << p1 << " before " << p2 << std::endl;
    }
    else
    {
        std::wcout << p2 << " before " << p1 << std::endl;
    }
}

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

    for (std::vector<std::wstring>::const_iterator strIt = locals.begin();
            strIt != locals.end(); ++strIt)
    {
        std::wcout << "In the " << *strIt << " locale: ";
        try_compare(std::locale(stows(*strIt)), L"år", L"ängel");
    }

    return 0;
}

输出

In the de-DE_phoneb locale: 鋘gel before 錼
In the es-ES_tradnl locale: 鋘gel before 錼
In the hu-HU_technl locale: 錼 before 鋘gel
In the ja-JP_radstr locale: 鋘gel before 錼
In the ka-GE_modern locale: 鋘gel before 錼
In the x-IV_mathan locale: 鋘gel before 錼
In the zh-CN_phoneb locale: 鋘gel before 錼
In the zh-CN_stroke locale: 鋘gel before 錼
In the zh-HK_radstr locale: 鋘gel before 錼
In the zh-MO_radstr locale: 鋘gel before 錼
In the zh-MO_stroke locale: 鋘gel before 錼
In the zh-SG_phoneb locale: 鋘gel before 錼
In the zh-SG_stroke locale: 鋘gel before 錼
In the zh-TW_pronun locale: 鋘gel before 錼
In the zh-TW_radstr locale: 鋘gel before 錼

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值