学习要点:#include <iomanip> 推荐用控制符啊
1 最小宽度 setw(12) 一次有效 默认右对齐
2 oct dec hex 设置进制 ,一直有效
3 显示进制 showbase
4 显示小数 showpoint
5 科学技术法 scientfic unscientfic
6 设置精度 setprecision(int) 普通表示法的有效位数 科学计数法的小数位数
7 左对齐 left 右对齐 right
8 设置填充 setfill(' ')
#include <iostream>
using namespace std;
#include <iomanip> //io控制符 setw() 需要 // io manipulator
int main()
{
cout.width(10); //制定最小宽度 一次性效果
cout << 123 << "," << 123 << endl;
cout.width(2); //最小2,但实际比2大
cout << 123 << "," << hex << 123 << ","<< 123 << endl;
//hex 16 进制 一直有效
//向设置成 八进制 oct , 10进制 dec
cout << oct << 123 << dec << endl;
//直接写在输出流里控制格式的东西叫格式控制符 hex setw() endl
cout << setw(10) << 123 << ","<< 123 << endl;//也是设置宽度 一次性效果
cout.setf(ios::hex,ios::basefield);//oct,hex 函数方式设置进制 比较麻烦
//使用格式控制符也可以输出showbase
cout << showbase <<123 << endl; //showbase 输出进制前缀 八进制带0 十六进制带0x 十进制不带
cout.unsetf(ios::hex); //取消hex标志
cout << 123 << endl;
//小数
cout << 123.45 << endl; //123.45
cout << 123.0 << endl; //输出123
//希望可以输出小数 控制符 showpoint 有效数字6位 超出四舍五入 一直有效
cout << showpoint << 123.0 << "," << 3.0 <<endl; //123.000 3.00000
cout << 123.55555<< endl; //123.556
//科学计算法 小数点后6位
cout << scientific << 95.0 << endl; // 设置科学计数法 一直有效9.500000e+01
cout.precision(3); //精度设置为3 科学计数法小数点后3位。普通是有效数字位数 一直有效
cout << 95.0 << "," << 123.0 << endl;//9.500e+01 ,1.230e+02
cout.unsetf(ios::scientific);//取消科学计数法
cout << 95.0 << endl; //95.0
cout << fixed << 95.0 << endl; //fixed就是用一般的方式输出浮点数 95.000
cout << setprecision(2) << 8000.0 <<endl; //格式控制符 设置精度 8000.00
//显示符号
cout << showpos << 123 << "," << -45.6 << endl; //+123,-45.60
//不显示符号
//cout << noshowpos << 123 << endl; //为了看后面的对齐
//大写 只管 十六进制0x的x 和 科学计数法的e !! ! 对普通字符串无效!
cout<< uppercase << hex << 123 << ", " <<scientific << 95.0 << ", " << "hello " << endl;
//0X7B, +9.50E+01, hello
cout.fill('#'); //设置填充字符 一直有效
//对齐 默认靠右对齐 或者 用right 一直有效
cout << dec << setw(10) << 123 << endl;
// left 左对齐 setfill() 设置填充
cout << setfill('*') << left << setw(10) << 123 << endl;
//两侧对齐
cout << setfill(' ') << internal << setw(10) << 123 <<endl;
/*
######+123
+123******
+ 123
*/
//设置右对齐
cout << right << setw(10) << 123 <<endl;
//不要缓冲
cout << unitbuf << "hello";
}