放牧代码和思想
专注自然语言处理、机器学习算法
    This thing called love. Know I would've. Thrown it all away. Wouldn't hesitate.

cout格式化输出 详解

//<iostream>在使用setf等库函数时使用
//<iomanip>在使用流操纵算子时使用
//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
/*
*dec, to insert or extract integer values in decimal format.
*hex, to insert or extract integer values in hexadecimal format.
*oct, to insert or extract integer values in octal format.
*showbase, to insert a prefix that reveals the base of a generated integer field.
*internal, to pad to a field width as needed by inserting fill characters at a point internal to a generated numeric field. (For information on setting the field width, see setw).
*left, to pad to a field width as needed by inserting fill characters at the end of a generated field (left justification).
*right, to pad to a field width as needed by inserting fill characters at the beginning of a generated field (right justification).
*boolalpha, to insert or extract objects of type bool as names (such as true and false) rather than as numeric values.
*fixed, to insert floating-point values in fixed-point format (with no exponent field).
*scientific, to insert floating-point values in scientific format (with an exponent field).
*showpoint, to insert a decimal point unconditionally in a generated floating-point field.
*showpos, to insert a plus sign in a nonnegative generated numeric field.
*skipws, to skip leading white space before certain extractions.
*unitbuf, to flush output after each insertion.
*uppercase, to insert uppercase equivalents of lowercase letters in certain insertions.
——————————–
*adjustfield, a bitmask defined as internal | left | right
*basefield, defined as dec | hex | oct
*floatfield, defined as fixed | scientific
*/

=====================================================================

知识共享许可协议 知识共享署名-非商业性使用-相同方式共享码农场 » cout格式化输出 详解

评论 欢迎留言

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

我的作品

HanLP自然语言处理包《自然语言处理入门》