locale本地化库学习

std::locale 类型的对象(本地环境对象)是不可变刻面的一个不可变索引集。C++ 输入/输出库的每个流对象都与一个 std::locale 对象关联,并用它的各刻面来分析及格式化所有数据。另外,每个 std::basic_regex 对象也都与一个本地环境对象关联。 (C++11 起)本地环境对象也可以在标准容器和算法中用作进行字符串校排的谓词,而且也可以直接访问,以获得或修改它所保有的平面。

成员函数

|-------------------------------------------------------------------------------------------------------------------------|------------------------------------|
| operator= | 替换本地环境 (公开成员函数) |
| combine | 以复制自另一本地环境的编译时鉴别的刻面构造本地环境 (公开成员函数) |
| name | 返回本地环境的名称,或若它无名则为 "*" (公开成员函数) |
| operator==operator!= (C++20 中移除) | 本地环境对象之间的相等性比较 (公开成员函数) |
| operator() | 用此本地环境的校排刻面以字典序比较两个字符串 (公开成员函数) |
| global [静态] | 更改全局本地环境 (公开静态成员函数) |

示例代码:

cpp 复制代码
#include <iostream>
#include <locale>
#include <string>         // std::string
#include <algorithm>      // std::sort
#include <vector>
#include <assert.h>

int main()
{
	//name example
	std::locale loc(std::locale(), new std::ctype<char>);
	std::cout << "默认本地环境是 " << std::locale().name() << '\n'
		<< "用户的本地环境是 " << std::locale("").name() << '\n'
		<< "一个无名本地环境是 " << loc.name() << '\n';
	//combine example
	const double number = 1000.25;
	std::cout << "\'C\' 本地环境:" << number << '\n';
	std::locale loc2 = std::locale().combine<std::numpunct<char>>(std::locale("en_US.UTF8"));
	std::cout.imbue(loc2);
	std::cout << "带有 en_US numpunct 的 \"C\" 本地环境:" << number << '\n';

	//global example
	std::wcout << "用户偏好的本地环境设置是" << std::locale("").name().c_str() << '\n';
	// 在启动时,全局本地环境是 "C" 本地环境
	std::wcout << 1000.01 << '\n';
	// 以用户偏好的本地环境替换 C++ 全局本地环境和 C 本地环境
	std::locale::global(std::locale("en_US.UTF8"));  //设置全局环境
	// 将来的宽字符输出会使用新的全局本地环境
	std::wcout.imbue(std::locale());
	// 再次输出同一数字
	std::wcout << 1000.01 << '\n';
	std::locale loc3;
	std::cout << "loc3.name()==================== " << loc3.name() << '\n';  //en_US.UTF8

	// locale::operator!= example
	if (std::cout.getloc() != std::locale("C"))
		std::cout << "cout is not using the C locale.\n";
	else
		std::cout << "cout is using the C locale.\n";

	// locale::operator() example
	std::string mystr[] = { "light","zoo","apple" };
	std::locale loc4;  // default global locale
	std::cout << std::boolalpha;

	// implicit call to locale::operator() (using operator)
	std::cout << mystr[0] << " < " << mystr[1] << " ? ";
	std::cout << (loc4(mystr[0], mystr[1])) << '\n';

	// explicit call to locale::operator() (using functional call)
	std::cout << mystr[1] << " < " << mystr[2] << " ? ";
	std::cout << (loc4.operator()(mystr[1], mystr[2])) << '\n';

	// locale::operator() as comparison predicate for algorithm:
	std::sort(mystr, mystr + 3, loc4);
	std::cout << "sorted sequence:";
	for (int i = 0; i < 3; i++) std::cout << ' ' << mystr[i];
	std::cout << '\n';

	// locale::operator= example
	std::locale loc5;
	std::cout << "loc5.name()==================== " << loc5.name() << '\n';  //en_US.UTF8
	loc5 = std::locale("ru_RU.UTF8");
	std::cout << "loc5.name()==================== " << loc5.name() << '\n';

	// locale::operator== example
	if (loc5 == std::locale("ru_RU.UTF8"))
		std::cout << "loc5 is using the ru_RU.UTF8 locale.\n";
	else
		std::cout << "loc5 is not using the ru_RU.UTF8 locale.\n";

	std::vector<std::wstring> v = { L"жил", L"был", L"кот" };
	std::sort(v.begin(), v.end(), std::locale("ru_RU.UTF8"));
	assert(v[0] == L"был");
	assert(v[1] == L"жил");
	assert(v[2] == L"кот");

	return 0;
}

运行结果:

参考:

https://cplusplus.com/reference/locale/locale/

标准库头文件 <locale> - cppreference.com

相关推荐
zengy51 小时前
Effective C++中文版学习记录(三)
数据结构·c++·学习·stl
cdut_suye1 天前
STL之list篇(下)(从底层分析实现list容器,逐步剥开list的外表)
开发语言·数据结构·c++·学习·算法·stl·list
ZZZ_O^O3 天前
暴力数据结构——AVL树
开发语言·数据结构·c++·算法·stl·avl树
zengy54 天前
从零开始手写STL库:multimap
开发语言·数据结构·c++·stl
Stark、5 天前
C++模拟实现vector容器【万字模拟✨】
开发语言·c++·后端·stl·vector·学习方法·模拟实现
cdut_suye7 天前
STL之list篇(上)初识list容器,了解其核心机制,实例化对象进行分析
开发语言·c++·学习·算法·stl·list
Lenyiin8 天前
《 C++ 修炼全景指南:十三 》为什么你的代码不够快?全面掌控 unordered_set 和 unordered_map 的哈希性能飙升魔法
数据结构·c++·stl·哈希算法
烦躁的大鼻嘎8 天前
二叉搜索树
数据结构·c++·算法·stl
阑梦清川11 天前
C++容器list底层迭代器的实现逻辑~list相关函数模拟实现
开发语言·c++·迭代器·stl
愿天垂怜11 天前
【C++】模板进阶
c语言·开发语言·c++·leetcode·stl·模板方法模式·string