std::locale
类型的对象(本地环境对象)是不可变刻面的一个不可变索引集。C++ 输入/输出库的每个流对象都与一个 std::locale
对象关联,并用它的各刻面来分析及格式化所有数据。另外,每个 std::basic_regex 对象也都与一个本地环境对象关联。 (C++11 起)本地环境对象也可以在标准容器和算法中用作进行字符串校排的谓词,而且也可以直接访问,以获得或修改它所保有的平面。
成员函数
operator= | 替换本地环境 (公开成员函数) |
combine | 以复制自另一本地环境的编译时鉴别的刻面构造本地环境 (公开成员函数) |
name | 返回本地环境的名称,或若它无名则为 "*" (公开成员函数) |
operator==operator!= (C++20 中移除) | 本地环境对象之间的相等性比较 (公开成员函数) |
operator() | 用此本地环境的校排刻面以字典序比较两个字符串 (公开成员函数) |
global [静态] | 更改全局本地环境 (公开静态成员函数) |
示例代码:
#include <iostream>
#include <locale>
#include <string> // std::string
#include <algorithm> // std::sort
#include <vector>
#include <assert.h>int main()
{//name examplestd::locale loc(std::locale(), new std::ctype<char>);std::cout << "默认本地环境是 " << std::locale().name() << '\n'<< "用户的本地环境是 " << std::locale("").name() << '\n'<< "一个无名本地环境是 " << loc.name() << '\n';//combine exampleconst 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 examplestd::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!= exampleif (std::cout.getloc() != std::locale("C"))std::cout << "cout is not using the C locale.\n";elsestd::cout << "cout is using the C locale.\n";// locale::operator() examplestd::string mystr[] = { "light","zoo","apple" };std::locale loc4; // default global localestd::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= examplestd::locale loc5;std::cout << "loc5.name()==================== " << loc5.name() << '\n'; //en_US.UTF8loc5 = std::locale("ru_RU.UTF8");std::cout << "loc5.name()==================== " << loc5.name() << '\n';// locale::operator== exampleif (loc5 == std::locale("ru_RU.UTF8"))std::cout << "loc5 is using the ru_RU.UTF8 locale.\n";elsestd::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