c++格式化输入输出
头文件及函数
#include<imoanip>
setw()
setfill()
fixed/scientific
setprecision()
##进制转换
## bool显示
用法
setw()
控制每一次输出的最小宽度,不足用空格替代
控制每一次输入的最大宽度
cout<<setw(10);
cout<<2<<endl
输出: 2
setfill()
设置填充字符如‘*‘、’-’以替代空格
cout<<setfill('*')
cout<<setw(20);
cout<<2<<endl;
输出:*******************2
fixed()&&scientific
scientific控制输出小数时用科学计数法(默认不用)
fixed控制小数点不动(后与setprecision连用)
cout<<scientific
cout<<1923.4212425423<<endl;
cout<<fixed
cout<<1923.4212425423<<endl;
输出:1.923421e+003
1923.421243
setprecision()
控制输出浮点数时的有效数字位数
与fixed连用就变成控制小数位数
cout<<setprecision(5);//有效数字是5位
cout<<1923.4212425423<<endl;
cout<<fixed<<setprecision(3);//3位小数
cout<<1923.4212425423<<endl;
输出:1923.4
1923.421
进制转换
int n = 42;
cout << hex << n << endl; // 十六进制: 2a
cout << oct << n << endl; // 八进制: 52
cout << dec << n << endl; // 恢复十进制
cin >> hex >>n;//输入十六进制数
cin >> oct >>n;//输入八进制数
cin >> dec >>n;//输入十进制数
boolalpha
显示true和false
cout<<boolalpha<<true<<endl;
输出:true
ws
输入时跳过空格
char c;
cin>>ws>>c;