ios_base& boolalpha (ios_base& str);
Sets the boolalpha format flag for the str stream.
When the boolalpha format flag is set, bool
values are inserted/extracted by their textual representation: either true
or false
, instead of integral values.
This flag can be unset with the noboolalpha manipulator.
For standard streams, the boolalpha flag is not set on initialization.
boolalpha是头文件#include <iostream>中的一个函数,是把bool a 变量打印出false或者true的函数.
使用如下:
#include <iostream>
using namespace std;
int main() {
bool a=true;
cout<<"请输入一个数值为布尔变量a赋值:"<<endl;
cout<<"=================分隔符=================="<<endl;
cout<<"若输入0则打印false,输入其他的数字均显示true:\n"<<endl;
cin>>a;
cout <<boolalpha<<a << endl;
cout<<"=================分隔符=================="<<endl;
return 0;
}
输出结果如图所示:
请输入一个数值为布尔变量a赋值:
=================分隔符==================
若输入0则打印false,输入其他的数字均显示true:
55
true
=================分隔符==================
进程已结束,退出代码 0
接下来,我们需要测试一下,在使用过一次boolalpha输出格式之后,后续的布尔值将会以何种形式输出:
include <iostream>
using namespace std;
int main() {
bool a=true;
cout<<"请输入一个数值为布尔变量a赋值:"<<endl;
cout<<"=================分隔符=================="<<endl;
cout<<"若输入0则打印false,输入其他的数字均显示true:\n"<<endl;
cin>>a;
cout <<boolalpha<<a << endl;
cout<<"=================分隔符=================="<<endl;
bool b=57;
cout<<"在使用boolalpha之后测试布尔值输出格式,b输出为:"<<b<<endl;
cout<<"测试使用noboolalpha来取消boolalpha效果:"<<noboolalpha<<b<<endl;
return 0;
}
输出结果为:
请输入一个数值为布尔变量a赋值:
=================分隔符==================
若输入0则打印false,输入其他的数字均显示true:
6
true
=================分隔符==================
在使用boolalpha之后测试布尔值输出格式,b输出为:true
测试使用noboolalpha来取消boolalpha效果:1
进程已结束,退出代码 0
由测试结果可以看出,在使用过一次boolalpa控制布尔值输出格式之后,后续也会按照该格式输出。
如果要取消boolalpa格式,则需要使用noboolalpa。