gpt4 book ai didi

C++ bool 返回 0 1 而不是 true false

转载 作者:IT老高 更新时间:2023-10-28 12:59:04 24 4
gpt4 key购买 nike

我重载了 equals(包括 == 和 !=),它检查两个对象是否相等,然后返回一个 boolean 值。

不幸的是,它打印的是 0 或 1。我知道它是正确的,但为了便于阅读,我无法弄清楚如何让它打印 true 或 false。

我什至尝试过:

if (a.equals(b))
{
return true;
}

return false;

但是,C++ 很顽固,输出 0 或 1。

任何帮助将不胜感激。

编辑 - 打印完成:

cout << "a == b is " << (a == b) << endl;

想要的输出是

a == b is true

最佳答案

您可以使用 std::boolalpha :

Sets the boolalpha format flag for the str stream.

When the boolalpha format flag is set, bool values are inserted/extracted as their names: true and false instead of integral values.

This flag can be unset with the noboolalpha manipulator.

The boolalpha flag is not set in standard streams on initialization.

std::cout.setf(std::ios::boolalpha);
std::cout << true;

std::cout << std::boolalpha << true;

关于C++ bool 返回 0 1 而不是 true false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8261674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com