C++打印布尔值,显示什么?

2022-01-19 00:00:00 boolean c++ cout

我将 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 时,它??们将显示为 01.当它为真时,它们将显示为 falsetrue.

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 处理数字转换的类别,因此如果您使用正确的语言环境灌输流,它可以/将打印出 truefalse,因为它们在其中表示语言环境.例如,

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

相关文章