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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:55:54  来源:igfitidea点击:

C++ printing boolean, what is displayed?

c++booleancout

提问by user788171

I print a boolto 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. 0for false)?

标准是否需要流上的特定结果(例如0for false)?

回答by Jerry Coffin

The standard streams have a boolalphaflag that determines what gets displayed -- when it's false, they'll display as 0and 1. When it's true, they'll display as falseand true.

标准流有一个boolalpha标志来决定显示什么——当它为假时,它们将显示为0and 1。当它为真时,它们将显示为falsetrue

There's also an std::boolalphamanipulator 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 boolalphais set to true is localized--that is, <locale>has a num_putcategory that handles numeric conversions, so if you imbue a stream with the right locale, it can/will print out trueand falseas they're represented in that locale. For example,

对于它的价值,boolalpha设置为 true时产生的实际单词是本地化的——也就是说,<locale>有一个num_put处理数字转换的类别,所以如果你用正确的语言环境注入流,它可以/将打印出来truefalse就像它们一样在该语言环境中重新表示。例如,

#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 fauxinstead 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 falsefor every language I've checked.

...至少在理论上(假设您的编译器/标准库接受“fr”作为“法语”的标识符)它可能会打印出来faux而不是false. 然而,我应该补充一点,对此的真正支持充其量是不平衡的——即使是 Dinkumware/Microsoft 库(在这方面通常非常好)打印false出我检查过的每种语言。

The names that get used are defined in a numpunctfacet though, so if you really want them to print out correctly for particular language, you can create a numpunctfacet to do that. For example, one that (I believe) is at least reasonably accurate for French would look like this:

使用的名称是在numpunctfacet中定义的,因此如果您真的希望它们针对特定语言正确打印出来,您可以创建一个numpunctfacet 来做到这一点。例如,(我相信)至少对法语来说相当准确的一个看起来像这样:

#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指的是1false指的是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

IDEONE

IDEONE