c++11 标准模板(STL)(std::locale)(五)用此 locale 的 collate 刻面以字典序比较两个字符串

用以封装文化差异的多态刻面的集合


std::locale 类型对象是不可变平面的不可变索引集。 C++ 输入/输出库的每个流对象与一个 std::locale 对象关联,并用其平面分析及格式化所有数据。另外, locale 对象与每个 std::basic_regex 对象关联。 locale 对象亦可在标准容器和算法中用作进行字符串对照的谓词,而且能被直接访问,以获得或修改其所保有的平面。

C++ 程序中构造的每个 locale 至少保有下列标准平面,但程序可以定义额外特化,或全新的平面,并将它们添加到任何既存的 locale 对象。

用此 locale 的 collate 刻面以字典序比较两个字符串

std::locale::operator()
template< class CharT, class Traits, class Alloc >

bool operator()( const basic_string<CharT,Traits,Alloc>& s1,

                 const basic_string<CharT,Traits,Alloc>& s2) const;

按照此 locale 的 std::collate<charT> 平面所定义的比较规则,比较二个 string 参数 s1s2 。此运算符允许以任何拥有 collate 平面的 locale 对象为标准算法(如 std::sort )和有序容器( std::set )中的二元谓词。

参数

s1 - 要比较的第一字符串
s2 - 要比较的第二字符串

返回值

s1 按字典序小于 s2 则为 true ,否则为 false 。

可能的实现

template<class CharT, class Traits, class Alloc >
bool operator()(const std::basic_string<CharT,Traits,Alloc>& s1,
                const std::basic_string<CharT,Traits,Alloc>& s2) const;
{
    return std::use_facet<std::collate<CharT>>(*this).compare(
                                         s1.data(), s1.data() + s1.size(),
                                         s2.data(), s2.data() + s2.size()   ) < 0;
}

调用示例

#include <locale>
#include <algorithm>
#include <vector>
#include <string>
#include <cassert>
#include <iostream>
#include <codecvt>

int main()
{
    std::vector<std::wstring> vector1 = {L"жил", L"был", L"кот"};
    std::sort(vector1.begin(), vector1.end(), std::locale("Chinese (Simplified)_China.936"));
    assert(vector1[0] == L"был");
    assert(vector1[1] == L"жил");
    assert(vector1[2] == L"кот");

    assert(vector1[0] != L"был");
    assert(vector1[1] != L"жил");
    assert(vector1[2] != L"кот");

    return 0;
}

输出

Assertion failed: vector1[0] != L"был", file ..\..\qt_code\locale\main.cpp, line 17

更改全局本地环境

std::locale::global

static locale global( const locale& loc );

loc 替换全局 C++ 本地环境,这表示将来所有对 std::locale 的默认构造函数的调用将返回 loc 的副本。若 loc 拥有名称,则亦如同用 std::setlocale(LC_ALL, loc.name().c_str()); 替换 C 本地环境。此函数是修改全局 C++ 本地环境的唯一方式,否则全局 C++ 本地环境等价于程序启动时的 std::locale::classic() 。

参阅

loc - 新的全局 C++ locale

返回值

全局 C++ locale 的先前值。

获得到 "C" 本地环境的引用

std::locale::classic

static const locale& classic();

获得到实现经典 "C" 本地环境语义的 C++ 本地环境的引用。此本地环境不同于全局本地环境,不能改变。

参数

(无)

返回值

返回到 "C" 本地环境的引用。

注意

一些标准要求的平面,如 UTF-8/UTF-32 转换平面 std::codecvt<char32_t, char, std::mbstate_t> ,无 "C" 本地环境中的等价版本,然而它们存在于 std::locale::classic() 返回的 locale 中,同任何其他构造于 C++ 程序中的 locale 。

调用示例

#include <locale>
#include <clocale>
#include <algorithm>
#include <vector>
#include <string>
#include <cassert>
#include <iostream>
#include <codecvt>

int main()
{
    std::locale locale1 = std::locale::classic();
    std::cout << "locale1.name():   " << locale1.name() << std::endl;

    std::locale::global(std::locale("Chinese (Simplified)_China.936"));
    std::locale locale2 = std::locale::classic();
    std::cout << "locale2.name():   " << locale2.name() << std::endl;

    return 0;
}

输出

locale1.name():   C
locale2.name():   C

相关推荐

  1. C语言 比较字符串

    2024-03-15 09:44:03       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 09:44:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 09:44:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 09:44:03       87 阅读
  4. Python语言-面向对象

    2024-03-15 09:44:03       96 阅读

热门阅读

  1. ChatGPT创造力与创新探究

    2024-03-15 09:44:03       42 阅读
  2. Hive连接函数 concat 和 concat_ws 使用示例

    2024-03-15 09:44:03       36 阅读
  3. 如果保障服务器的安全

    2024-03-15 09:44:03       43 阅读
  4. ubuntu服务器使用netplan管理工具添加静态地址

    2024-03-15 09:44:03       34 阅读
  5. C++ lambda函数个人理解

    2024-03-15 09:44:03       44 阅读
  6. springboot配置文件Tomcat和mvc详细配置

    2024-03-15 09:44:03       37 阅读
  7. 面向对象设计之里氏替换原则

    2024-03-15 09:44:03       41 阅读
  8. SqlServer 系统表

    2024-03-15 09:44:03       44 阅读
  9. 本地环境下运行Spark程序

    2024-03-15 09:44:03       43 阅读
  10. Python和MATLAB数字信号波形和模型模拟

    2024-03-15 09:44:03       46 阅读
  11. 90%的程序员不适合做独立开发

    2024-03-15 09:44:03       41 阅读
  12. 这个不需要吗 HttpServletRequest req

    2024-03-15 09:44:03       48 阅读