C++ bool returns 0 1 instead of true false(C++ bool 返回 0 1 而不是 true false)
问题描述
我重载了 equals(包括 == 和 !=),它检查两个对象是否相等,然后返回一个布尔值.
I have overloaded equals (including == and !=) that checks if two objects are equals and then returns a boolean.
不幸的是,它打印的是 0 或 1.我知道它是正确的,但为了便于阅读,我无法弄清楚如何让它打印 true 或 false.
Unfortunately, it prints 0 or 1. I know it's correct but I can't figure out the way to make it to print true or false for readability purposes.
我什至尝试过:
if (a.equals(b))
{
return true;
}
return false;
但是,C++ 很顽固,输出 0 或 1.
However, C++ is stubborn enough to output 0 or 1.
任何帮助将不胜感激.
编辑 - 打印完成:
cout << "a == b is " << (a == b) << endl;
想要的输出是
a == b 为真
推荐答案
你可以使用std::boolalpha:
为 str 流设置 boolalpha 格式标志.
Sets the boolalpha format flag for the str stream.
当设置了 boolalpha 格式标志时,bool 值是插入/提取为他们的名字:真假而不是整数价值观.
When the boolalpha format flag is set, bool values are inserted/extracted as their names: true and false instead of integral values.
可以使用 noboolalpha 操纵器取消设置此标志.
This flag can be unset with the noboolalpha manipulator.
初始化时标准流中未设置 boolalpha 标志.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ bool 返回 0 1 而不是 true false
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
