zoukankan      html  css  js  c++  java
  • cout格式化输出 详解

    //在使用setf等库函数时使用
    //在使用流操纵算子时使用
    //using namespace std;
    //以下所有的setf()都有对应的unsetf()用于取消设置
    //所有的setiosflags()可以用resetiosflags()取消
    //标志位fmtflags的命名空间可以使用ios_base::或者ios:: int laneri = 12345; double lanerd = 56789;
    //1、设置整数进制输出
    //重载1:fmtflags ios_base::setf(fmtflags _Mask);
    //重载2:fmtflags ios_base::setf(fmtflags _Mask, fmtflags _Unset);
    //使用重载1的时候,一定要先取消当前基,之后才可以设置新的基
    //使用重载2的时候,第二个参数设为当前的基,或者当不知道当前基时,设为ios_base::basefield清除当前的
    //所有可能的基
    //可使用的标志:ios::dec, ios::oct, ios::hex, ios::basefield(= dec|oct|hex)
    cout.unsetf(ios::dec);   //等价1 cout.setf(ios::hex);
    cout.setf(ios::hex, ios_base::basefield); //等价2 cout<<laneri<<endl;
    cout<<setiosflags(ios::hex)<<laneri<<endl; //等价3 cout<<std::hex<<laneri<<endl;   //等价4
    //使用输入输出操纵符也能有等价效果(命名空间使用std::,否则会有多余的字符),注意这种方法其实不止对
    //本句生效
    //2、 显示进制前导字符(0、0x) cout.setf(ios::showbase);
    cout<<setiosflags(ios::showbase)<<laneri<<endl; cout<<std::showbase<<laneri<<endl;
    //3、使用科学记数法
    //只对数据类型为小数的变量有效(或者字面值是小数) //对precision有影响(详见precision的说明)
    //对ios::fixed有影响(详见fixed的说明),但不会被fixed影响 cout.setf(ios::scientific); cout<<lanerd<<endl;
    cout<<setiosflags(ios::scientific)<<lanerd<<endl; cout<<std::scientific<<lanerd<<endl;
     
     
    //4、设置小数的浮点/定点显示方式
    //主要依靠precision体现(详见precision的说明)
    //当设置了ios::scientific标志时,ios::fixed会受到影响,std::fixed不会
    cout.setf(ios::fixed);  cout<<lanerd<<endl;
    cout<<setiosflags(ios::fixed)<<lanerd<<endl;  cout<<std::fixed<<lanerd<<endl;
    //5、设置小数数据类型的显示精度,受到scientific和fixed的影响 //当设置(fixed | scientific)时,precision(n)表示小数点后固定显示n位小数
    //当不设置(fixed & scientific)时,precision(n)表示固定显示n位数字 // 其中,当是整数且位数m小于n,而又没有设置showpoint的时候,只显示m位整数。例如:precision(3),12->12
    // 其中,当是整数且位数p大于n,无论设置showpoint与否,都四舍五入后使用科学计数法。例如:precision(3),1234->1.23e+003 cout.precision(3); cout<<lanerd<<endl;
    cout<<setprecision(3)<<3.1415926<<endl;
    //6、强制浮点数类型变量的小数点显示 
    //如果是整数,大于precision宽度时使用科学计数法,小于precision则小数点后面补0,等于precision时显示
    //小数点但无小数 
    //例:不设fixed,precision(6):
    1234567->1.23457E+006;   12345->12345.0;    123456->123456. 
    //  设fixed,precision(6):
    1234567->1234567.000000; 12345->12345.000000; 123456->123456.000000 cout.setf(ios::showpoint);
    cout<<setiosflags(ios::showpoint)<<lanerd<<endl; cout<<std::showpoint<<lanerd<<endl;
    //7、设置屏幕上的最小显示宽度
    //实际字符数大于等于这个数字,显示全部;小于这个数字,用fill()设置的字符来填充其他占位符
    //注意:宽度设置只对下一个"<<"输出有效 
    //例如:cout<<setw(10)<<right<<"laner"<<"linke";只有"laner"是占10个字符,linke不是 cout.width(12);
    //cout<<setw(12)<<3.14<<endl;
    //8、显示对齐方式,默认为左对齐 cout.setf(ios::right);
    cout<<setiosflags(ios::right)<<laneri<<endl;
    cout<<std::right<<6.28<<endl;
    //9、设置不足显示宽度时的填充字符,默认为' ' cout.fill('*');
    cout<<setfill('$')<<laneri<<endl;
    ===================================================================== //附:ios_base::fmtflags
    =====================================================================
  • 相关阅读:
    HDU 1261 字串数(排列组合)
    Codeforces 488C Fight the Monster
    HDU 1237 简单计算器
    POJ 2240 Arbitrage
    POJ 3660 Cow Contest
    POJ 1052 MPI Maelstrom
    POJ 3259 Wormholes
    POJ 3268 Silver Cow Party
    Codesforces 485D Maximum Value
    POJ 2253 Frogger(最短路)
  • 原文地址:https://www.cnblogs.com/tankeee/p/3957632.html
Copyright © 2011-2022 走看看