#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
/*1. 常用流状态
showpos 在正数(包括0)之前显示+号
showbase 十六进制整数前加0X,八进制整数前加0
uppercase 十六进制格式字母用大写字母表示(默认为小写字母)
showpoint 浮点输出即使小数点后都为0也加小数点
boolalpha 逻辑值1和0用true和false表示
left 左对齐(填充字符填在右边)
right 右对齐(填充字符填在左边)
dec 十进制显示整数
hex 十六进制显示整数
oct 八进制显示整数
fixed 定点数格式输出
scientific 科学计数法格式输出
取消流状态的操作为:
noshowpos,noshowbase,nouppercase,noshowpoint,noboolalpha
*/
cout << showpos << 12 << endl;
cout << hex << 18 << " " << showbase << 18 << endl;
cout << hex << 255 << " " << uppercase << 255 << endl;
cout << 123.0 << " " << showpoint << 123.0 << endl;
cout << (2 > 3) << " " << boolalpha << (2 > 3) << endl;
cout << fixed << 12345.678 << endl;
cout << scientific << 12345.678 << endl;
cout.unsetf(ios::scientific);
/*
有参数的三个常用的流状态
width(int) //设置显示宽度
fill(int) //设置填充字符
precision(int) //设置有效位数(普通显示方式)或精度(定点或科学计数法方式)
这些流状态是以cout捆绑调用它们的形式设置的,不能与流出符<<连用。
特别注意width(n)为一次性操作,即第二次显示时将不再有效。
默认为width(0),表示仅显示数值。例如如下例子
*/
cout.width(5);
cout.fill('S');
cout << dec << noshowpos << 23 << 23 << endl; //输出 :SSS2323
/*
3. 与 << 连用的设置方式
还有另一种与流出符 << 连用的设置方式,但在使用时,要包含另一个头文件 iomanip
setw(int)
setfill(char)
setprecision(int)
如下例子
*/
cout << setw(6) << setfill('$') << 27 << endl; // 输出 $$$$27
cout << setfill('M') << setw(10) << 'M' << endl;//输出10个M
system("pause");
return 0;
}
/*第1部分输出
+12
12 0x12
0xff 0XFF
+123 +123.000
0 false
+12345.678000
+1.234568E+004
请按任意键继续. . .
*/
/*
left 和 right 是对立的,设置了此,就取消了彼。dec/oct/hex三者也是互相独立的,设置了此,取消了彼
fixed与scientific和一般显示方式三者也是独立的,不过它们的取消方式比较不同,
为cout捆绑函数调用的方式:
cout.unsetf(ios::scientific);
*/
C++标准IO流
猜你喜欢
转载自blog.csdn.net/qiukapi/article/details/105235132
今日推荐
周排行