C ++打印布尔值,显示什么? [英] C++ printing boolean, what is displayed?
问题描述
我将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
标志,该标志确定要显示的内容-如果为假,它们将显示为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<<"\n";
std::cout << std::boolalpha;
std::cout<<false<<"\n";
return 0;
}
...产生如下输出:
...produces output like:
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 << "\n";
std::cout << std::boolalpha;
std::cout << false << "\n";
return 0;
}
...并且至少在理论上(假设您的编译器/标准库接受"fr"作为法语"的标识符),它可能会打印出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 << "\n";
std::cout << std::boolalpha;
std::cout << false << "\n";
return 0;
}
结果是(如您可能预期的那样):
And the result is (as you'd probably expect):
0
faux
这篇关于C ++打印布尔值,显示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!