C++ 打印布尔值,显示什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15960015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ printing boolean, what is displayed?
提问by user788171
I print a bool
to an output stream like this:
我将 a 打印bool
到这样的输出流:
#include <iostream>
int main()
{
std::cout << false << std::endl;
}
Does the standard require a specific result on the stream (e.g. 0
for false
)?
标准是否需要流上的特定结果(例如0
for false
)?
回答by Jerry Coffin
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
.
标准流有一个boolalpha
标志来决定显示什么——当它为假时,它们将显示为0
and 1
。当它为真时,它们将显示为false
和true
。
There's also an std::boolalpha
manipulator to set the flag, so this:
还有一个std::boolalpha
操纵器来设置标志,所以这个:
#include <iostream>
#include <iomanip>
int main() {
std::cout<<false<<"\n";
std::cout << std::boolalpha;
std::cout<<false<<"\n";
return 0;
}
...produces output like:
...产生如下输出:
0
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,
对于它的价值,boolalpha
设置为 true时产生的实际单词是本地化的——也就是说,<locale>
有一个num_put
处理数字转换的类别,所以如果你用正确的语言环境注入流,它可以/将打印出来true
,false
就像它们一样在该语言环境中重新表示。例如,
#include <iostream>
#include <iomanip>
#include <locale>
int main() {
std::cout.imbue(std::locale("fr"));
std::cout << false << "\n";
std::cout << std::boolalpha;
std::cout << false << "\n";
return 0;
}
...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.
...至少在理论上(假设您的编译器/标准库接受“fr”作为“法语”的标识符)它可能会打印出来faux
而不是false
. 然而,我应该补充一点,对此的真正支持充其量是不平衡的——即使是 Dinkumware/Microsoft 库(在这方面通常非常好)打印false
出我检查过的每种语言。
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:
使用的名称是在numpunct
facet中定义的,因此如果您真的希望它们针对特定语言正确打印出来,您可以创建一个numpunct
facet 来做到这一点。例如,(我相信)至少对法语来说相当准确的一个看起来像这样:
#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 ""; }
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 << "\n";
std::cout << std::boolalpha;
std::cout << false << "\n";
return 0;
}
And the result is (as you'd probably expect):
结果是(如您所料):
0
faux
回答by Ritesh Kumar Gupta
0 will get printed.
0 将被打印。
As in C++ truerefers to 1and falserefers to 0.
在 C++ 中true指的是1而false指的是0。
In case, you want to print falseinstead of 0,then you have to sets the boolalpha format flag for the str stream.
如果您想打印false而不是0,则必须为 str 流设置 boolalpha 格式标志。
When the boolalpha format flag is set, bool values are inserted/extracted by their textual representation: either true or false, instead of integral values.
当 boolalpha 格式标志被设置时,bool 值通过它们的文本表示插入/提取:true 或 false,而不是整数值。
#include <iostream>
int main()
{
std::cout << std::boolalpha << false << std::endl;
}
output:
输出:
false