今天花了半天整这个东西,参考http://www.cnblogs.com/walfud/articles/2047096.html 这个网页的内容总结了一下。大部分内容参考该网页
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//numeric(数值),literal(字面值)
cout << true << " or " << false << endl; //输出1 or 0 默认数值
cout << boolalpha << true << " or " << false << endl; //输出true or false 改为字面值
cout << true << " or " << false << endl; //还是输出 true or false 说明是持续生效的
cout << 0 << endl; //输出0
cout << boolalpha << 0 << endl; //输出0,因为0不等价于false
cout << noboolalpha << true << " or " << false << endl; //恢复输出1 or 0 恢复数值
cout << "-------------------------------------------------" << endl;
const int ival = 17; // 常量,不允许改变
cout << "oct : " << oct << ival << endl; // 21 : 8 进制
cout << "dec : " << dec << ival << endl; // 17 : 10 进制
cout << "hex : " << hex << ival << endl; // 11 : 16 进制
cout << "hex : " << hex << 17.01 << endl; // 17.01 : 不受影响
cout << "----------------------------------------------------" << endl;
cout << showbase;
cout << "oct : " << oct << ival << endl; // 21 : 8 进制
cout << "dec : " << dec << ival << endl; // 17 : 10 进制
cout << "hex : " << hex << ival << endl; // 11 : 16 进制
cout << "hex : " << hex << 17.01 << endl; // 17.01 : 不受影响
cout << "----------------------------------------------------" << endl;
cout << showbase << uppercase; //将十六进制符改为大写
cout << "hex: " << hex << 15 << endl;
cout << nouppercase; //又改回小写
cout << "hex: " << 15 << endl;
cout << noshowbase;
cout << 15 << endl; //输出 f 说明hex影响还在
cout << dec; // 恢复对整数用十进制输出
cout << "-----------------------------------------------------" << endl;
//对于double型的控制 缺省值默认六位数
// cout.precision(4); 等价于 cout<<setprecision(4)
cout << setprecision(2) << 12.345678 << endl; //把小数全舍掉
cout << setprecision(4) << 12.345678 << endl; // 12.35 四舍五入(rounded!)
cout << setprecision(9) << 12.345678 << endl; // 12.345678 其实内部发生了 rounded, 而结果正好进位, 与原值相同
cout << cout.precision() << endl; // 输出当前精度
cout << setprecision(6); //恢复默认值
cout << "-----------------------------------------------------" << endl;
float f = 101/6.0;
cout << f << endl;
cout << fixed << f << endl; //定点数输出,也就是说小数后6位小数(默认值也是)
cout << scientific << f << endl;
cout << f << endl; //依然受到scientific的影响
cout << fixed << f << endl;
cout.unsetf(ostream::floatfield); //恢复到默认值
cout << f << endl;
cout << scientific << f << endl; //科学计数法也默认 系数用定点数表示
cout.unsetf(ostream::floatfield); //消除对下面的影响
cout << "-----------------------------------------------------" << endl;
cout << 10.0 << endl; // 10
cout << showpoint << 10.0 << endl; // 10.0000
cout << noshowpoint << 10.0<<endl; // 恢复到默认处理十进制 10
cout << "-----------------------------------------------------" << endl;
//输出填充
//setw(n)为后面一个数字设定宽度,默认右对齐
cout << setw(10) << 12.3 << endl; // ______12.3
cout << setw(10) << 12 << 3 << endl; // ________123
cout << setw(3) << 12.345 << endl; // 实际宽度大于设置宽度,设置宽度无效
cout << left; // 左对齐
cout << setw(5) << 12 << setw(5) << 34 << endl; // 12___34___
cout << right;
cout << setw(5) << 12 << setw(5) << 34 << endl; // ___12___34
cout << internal; // By default
cout << setw(5) << -12 << endl; // -__12
cout << setw(5) << 12 << endl; // ___12
cout << setfill('*'); // By default
cout << setw(5) << 12 << endl; // ***12
system("PAUSE");
return 0;
}