命名空间
变体
操作

std::locale

来自 cppreference.com
< cpp‎ | locale
 
 
 
 
定义在头文件 <locale>
class locale;

std::locale 类的对象是一个不可变的不可变面的索引集。C++ 输入/输出库的每个流对象都与一个 std::locale 对象相关联,并使用其面来解析和格式化所有数据。 此外,一个区域设置对象与每个 std::basic_regex 对象相关联。(自 C++11 起) 区域设置对象也可以用作谓词,它使用标准容器和算法执行字符串排序,并且可以直接访问以获取或修改它们包含的面。

在 C++ 程序中构造的每个区域设置都至少包含以下标准面(即 std::has_facet 返回 true 对于所有这些面类型),但程序可以定义额外的特化或完全新的面,并将它们添加到任何现有的区域设置对象中。

支持的面
std::ctype<char>
std::ctype<wchar_t>
std::codecvt<char, char, std::mbstate_t>
std::codecvt<wchar_t, char, std::mbstate_t>
std::num_get<char>
std::num_get<wchar_t>
std::numpunct<char>
std::numpunct<wchar_t>
std::num_put<char>
std::num_put<wchar_t>
std::money_get<char>
std::money_get<wchar_t>
std::moneypunct<char>
std::moneypunct<char, true>
std::moneypunct<wchar_t>
std::moneypunct<wchar_t, true>
std::money_put<char>
std::money_put<wchar_t>
std::time_get<char>
std::time_get<wchar_t>
std::collate<char>
std::collate<wchar_t>
std::time_put<char>
std::time_put<wchar_t>
std::messages<char>
std::messages<wchar_t>
已弃用的面
std::codecvt<char16_t, char, std::mbstate_t> (自 C++11 起)(在 C++20 中弃用)
std::codecvt<char32_t, char, std::mbstate_t> (自 C++11 起)(在 C++20 中弃用)
std::codecvt<char16_t, char8_t, std::mbstate_t> (自 C++20 起)(已弃用)
std::codecvt<char32_t, char8_t, std::mbstate_t> (自 C++20 起)(已弃用)

在内部,区域设置对象被实现为好像它是一个指向面的数组(按 std::locale::id 索引)的引用计数指针:复制区域设置只复制一个指针并增加几个引用计数。为了维护标准 C++ 库的线程安全保证(对不同对象的运算始终是线程安全的),区域设置引用计数和每个面引用计数都以线程安全的方式更新,类似于 std::shared_ptr

内容

[编辑] 成员类型

类型 描述
面索引类型:每个面类必须声明或继承此类型的公共静态成员
(类) [编辑]
所有面类别的基类:任何类别的每个面都从此类型派生
(类) [编辑]
category
int
(typedef)

[编辑] 成员常量

名称 解释
const category none
[静态]
表示没有面类别的一个零值
(公共静态成员常量)
const category collate
[静态]
表示排序面类别的位掩码值
(公共静态成员常量)
const category ctype
[静态]
表示字符类型面类别的位掩码值
(公共静态成员常量)
const category monetary
[静态]
表示货币面类别的位掩码值
(公共静态成员常量)
const category numeric
[静态]
表示数字面类别的位掩码值
(公共静态成员常量)
const category time
[静态]
表示时间面类别的位掩码值
(公共静态成员常量)
const category messages
[静态]
表示消息面类别的位掩码值
(公共静态成员常量)
const category all
[静态]
collate | ctype | monetary | numeric | time | messages
(公共静态成员常量)

std::locale 成员函数期望一个 category 参数,需要以上定义的类别值之一,或两个或多个此类值的并集。 LC 常量 不被接受。

[编辑] 成员函数

构造一个新的区域设置
(公共成员函数) [编辑]
析构区域设置和引用计数变为零的面
(公共成员函数) [编辑]
替换区域设置
(公共成员函数) [编辑]
使用从另一个区域设置复制的编译时标识的面构造一个区域设置
(公共成员函数) [编辑]
返回区域设置的名称,如果未命名则返回 "*"
(公共成员函数) [编辑]
(C++26)
返回与区域设置关联的字符编码方案
(公共成员函数) [编辑]
(在 C++20 中移除)
区域设置对象之间的相等比较
(公共成员函数) [编辑]
使用此区域设置的排序面按字典顺序比较两个字符串
(公共成员函数) [编辑]
[静态]
更改全局区域设置
(公共静态成员函数) [编辑]
[静态]
获取对“C”区域设置的引用
(公共静态成员函数) [编辑]

[编辑] 示例

演示了区域设置敏感程序的典型序言(跨平台)。

#include <iostream>
#include <locale>
 
int main()
{
    std::wcout << L"User-preferred locale setting is "
               << std::locale("").name().c_str() << L'\n';
    // on startup, the global locale is the "C" locale
    std::wcout << 1000.01 << L'\n';
 
    // replace the C++ global locale and the "C" locale with the user-preferred locale
    std::locale::global(std::locale(""));
    // use the new global locale for future wide character output
    std::wcout.imbue(std::locale());
 
    // output the same number again
    std::wcout << 1000.01 << L'\n';
}

可能的输出

User-preferred locale setting is en_US.UTF8
1000.01
1,000.01

[编辑] 缺陷报告

以下行为更改缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 应用于 发布的行为 正确的行为
LWG 340 C++98 所有区域设置都需要持有的标准面集不清楚 澄清
LWG 347 C++98 类型为 category 的参数可以接受 LC 常量 不再被接受

[编辑] 另请参阅

描述了访问 IANA 字符集注册表 的接口
(类) [编辑]
从区域设置中获取面
(函数模板) [编辑]
检查区域设置是否实现了特定面
(函数模板) [编辑]
设置区域设置
(std::ios_base 的公共成员函数) [编辑]
返回当前区域设置
(std::ios_base 的公共成员函数) [编辑]