cout将bool直接输出为true和false
//---------------------------------------
//使用boolalpha输出为bool类型
//使用noboolalpha输出为数字类型
//--------------------------------------
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
bool test = true;
cout << "the output is number " << test << endl;
cout << "the output is bool(use boolalpha) " << boolalpha << test << endl;
cout << "the output is number(use noboolalpha) " << noboolalpha << test << endl;
system("pause");
return 0;
}
输出:
the output is number 1
the output is bool(use boolalpha) true
the output is number(use noboolalpha) 1
请按任意键继续. . .
本文详细介绍了在C++中使用cout输出bool值的两种方式:直接输出为数字(0或1)和使用boolalpha输出为true或false。通过示例代码演示了如何在不同场景下灵活运用这些技巧。

2万+

被折叠的 条评论
为什么被折叠?



