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

成员函数

operator=

替换本地环境
(公开成员函数)

combine

以复制自另一本地环境的编译时鉴别的刻面构造本地环境
(公开成员函数)

name

返回本地环境的名称,或若它无名则为 “*”
(公开成员函数)

operator==operator!=

(C++20 中移除)

本地环境对象之间的相等性比较
(公开成员函数)

operator()

用此本地环境的校排刻面以字典序比较两个字符串
(公开成员函数)

global

[静态]

更改全局本地环境
(公开静态成员函数)

示例代码:

#include 
#include 
#include          // std::string
#include       // std::sort
#include 
#include 

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

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

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

	// 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])) << '
';

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

	// 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 << '
';

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

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

	std::vector 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;
}

运行结果:

locale本地化库学习插图

参考:

http://cplusplus.com/reference/locale/locale/

– cppreference.com”>标准库头文件 – cppreference.com

本站无任何商业行为
个人在线分享 » locale本地化库学习
E-->