命名空间
变体
操作

std::locale

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

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

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

支持的 facet
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>
已废弃的 facet
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 起)(已废弃)

在内部,locale 对象的实现方式,如同它是指向 facet 的引用计数指针数组(由 std::locale::id 索引)的引用计数指针。复制 locale 仅复制一个指针并增加多个引用计数。为了维护标准 C++ 库的线程安全保证(不同对象上的操作始终是线程安全的),locale 引用计数和每个 facet 引用计数都以线程安全的方式更新,类似于 std::shared_ptr

目录

[编辑] 成员类型

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

[编辑] 成员常量

名称 解释
const category none
[静态]
零值,表示没有 facet 类别
(public static 成员常量)
const category collate
[静态]
位掩码值,表示 collate facet 类别
(public static 成员常量)
const category ctype
[静态]
位掩码值,表示 ctype facet 类别
(public static 成员常量)
const category monetary
[静态]
位掩码值,表示 monetary facet 类别
(public static 成员常量)
const category numeric
[静态]
位掩码值,表示 numeric facet 类别
(public static 成员常量)
const category time
[静态]
位掩码值,表示 time facet 类别
(public static 成员常量)
const category messages
[静态]
位掩码值,表示 messages facet 类别
(public static 成员常量)
const category all
[静态]
collate | ctype | monetary | numeric | time | messages
(public static 成员常量)

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

[编辑] 成员函数

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

[编辑] 示例

演示了 locale 敏感程序(跨平台)的典型序言。

#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++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 340 C++98 所有 locale 都需要持有的标准 facet 集合不明确 已明确
LWG 347 C++98 category 类型的参数可以接受 LC 常量 不再接受

[编辑] 另请参阅

描述了用于访问 IANA 字符集注册表的接口
(类) [编辑]
从区域设置中获取一个刻面
(函数模板) [编辑]
检查区域设置是否实现了特定刻面
(函数模板) [编辑]
设置区域设置
(std::ios_base 的公共成员函数) [编辑]
返回当前区域设置
(std::ios_base 的公共成员函数) [编辑]
English 日本語 中文(简体) 中文(繁體)