C++ printing boolean, what is displayed?(C++打印布尔值,显示什么?)
问题描述
我将 bool
打印到这样的输出流中:
I print a bool
to an output stream like this:
#include <iostream>
int main()
{
std::cout << false << std::endl;
}
标准是否要求流上有特定的结果(例如 0
表示 false
)?
Does the standard require a specific result on the stream (e.g. 0
for false
)?
推荐答案
标准流有一个 boolalpha
标志,用于确定显示的内容——当它为 false 时,它们将显示为 0
和 1
.当它为真时,它们将显示为 false
和 true
.
The standard streams have a boolalpha
flag that determines what gets displayed -- when it's false, they'll display as 0
and 1
. When it's true, they'll display as false
and true
.
还有一个 std::boolalpha
操纵器来设置标志,所以这样:
There's also an std::boolalpha
manipulator to set the flag, so this:
#include <iostream>
#include <iomanip>
int main() {
std::cout<<false<<"
";
std::cout << std::boolalpha;
std::cout<<false<<"
";
return 0;
}
...产生如下输出:
0
false
不管怎样,当 boolalpha
设置为 true 时产生的实际单词是本地化的——也就是说,<locale>
有一个 num_put
处理数字转换的类别,因此如果您使用正确的语言环境灌输流,它可以/将打印出 true
和 false
,因为它们在其中表示语言环境.例如,
For what it's worth, the actual word produced when boolalpha
is set to true is localized--that is, <locale>
has a num_put
category that handles numeric conversions, so if you imbue a stream with the right locale, it can/will print out true
and false
as they're represented in that locale. For example,
#include <iostream>
#include <iomanip>
#include <locale>
int main() {
std::cout.imbue(std::locale("fr"));
std::cout << false << "
";
std::cout << std::boolalpha;
std::cout << false << "
";
return 0;
}
...至少在理论上(假设您的编译器/标准库接受fr"作为French"的标识符)它可能会打印出 faux
而不是 false代码>.然而,我应该补充一点,对此的真正支持充其量是不平衡的——即使是 Dinkumware/Microsoft 库(在这方面通常非常好)也为我检查过的每种语言打印
false
.
...and at least in theory (assuming your compiler/standard library accept "fr" as an identifier for "French") it might print out faux
instead of false
. I should add, however, that real support for this is uneven at best--even the Dinkumware/Microsoft library (usually quite good in this respect) prints false
for every language I've checked.
使用的名称是在 numpunct
方面定义的,所以如果您真的希望它们针对特定语言正确打印,您可以创建一个 numpunct
方面要做到这一点.例如,(我相信)至少对法语相当准确的一个看起来像这样:
The names that get used are defined in a numpunct
facet though, so if you really want them to print out correctly for particular language, you can create a numpunct
facet to do that. For example, one that (I believe) is at least reasonably accurate for French would look like this:
#include <array>
#include <string>
#include <locale>
#include <ios>
#include <iostream>
class my_fr : public std::numpunct< char > {
protected:
char do_decimal_point() const { return ','; }
char do_thousands_sep() const { return '.'; }
std::string do_grouping() const { return "3"; }
std::string do_truename() const { return "vrai"; }
std::string do_falsename() const { return "faux"; }
};
int main() {
std::cout.imbue(std::locale(std::locale(), new my_fr));
std::cout << false << "
";
std::cout << std::boolalpha;
std::cout << false << "
";
return 0;
}
结果是(如您所料):
0
faux
这篇关于C++打印布尔值,显示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++打印布尔值,显示什么?
基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01