std::numpunct<CharT>:: truename, do_truename, falsename, do_falsename
From cppreference.net
C++
Text processing library
| Localization library | |||||||||||||||||||||||||
| Regular expressions library (C++11) | |||||||||||||||||||||||||
| Formatting library (C++20) | |||||||||||||||||||||||||
| Null-terminated sequence utilities | |||||||||||||||||||||||||
| Byte strings | |||||||||||||||||||||||||
| Multibyte strings | |||||||||||||||||||||||||
| Wide strings | |||||||||||||||||||||||||
| Primitive numeric conversions | |||||||||||||||||||||||||
|
|||||||||||||||||||||||||
| Text encoding identifications | |||||||||||||||||||||||||
|
|||||||||||||||||||||||||
Localization library
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::numpunct
| Member functions | ||||
|
numpunct::truename
numpunct::do_truename
numpunct::falsename
numpunct::do_falsename
|
|
定义于头文件
<locale>
|
||
|
public
:
string_type truename ( ) const ; |
(1) | |
|
public
:
string_type falsename ( ) const ; |
(2) | |
|
protected
:
virtual string_type do_truename ( ) const ; |
(3) | |
|
protected
:
virtual string_type do_falsename ( ) const ; |
(4) | |
1,2)
公开成员函数,分别调用最终派生类的成员函数
do_truename
和
do_falsename
。
3)
返回用作布尔值
true
的字符串表示形式。
4)
返回用作布尔值
false
表示形式的字符串。
返回值
1,3)
用作
true
表示的
string_type
类型对象。
std::numpunct
的标准特化返回
"true"
和
L
"true"
。
2,4)
用作
false
表示的
string_type
类型对象。
std::numpunct
的标准特化返回
"false"
和
L
"false"
。
示例
运行此代码
#include <iomanip> #include <iostream> #include <locale> struct custom_tf : std::numpunct<char> { std::string do_truename() const { return {'t'}; } std::string do_falsename() const { return {'f'}; } }; int main() { std::cout << std::boolalpha; // 默认的 boolalpha 输出 std::cout << "默认区域设置,\n" " boolalpha 真值: " << true << "\n" " boolalpha 假值: " << false << "\n\n"; // 应用了 custom_tf 的区域设置 std::cout.imbue(std::locale(std::cout.getloc(), new custom_tf)); std::cout << "使用修改后的 numpunct 的区域设置,\n" " boolalpha 真值: " << true << "\n" " boolalpha 假值: " << false << '\n'; }
输出:
默认区域设置, boolalpha 真值: true boolalpha 假值: false 使用修改后的 numpunct 的区域设置, boolalpha 真值: t boolalpha 假值: f