黑马程序员---cout 的简单用法

来源:互联网 发布:cf安卓版刷枪软件 编辑:程序博客网 时间:2024/06/05 01:15

-----------android培训java培训、java学习型技术博客、期待与您交流!------------ 

cout格式化输出 详解 2010-04-28 21:46 

头文件(http://www.cplusplus.com/reference/sstream/)

//<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 */

-------------------------------- 

oid file_it(ostream & os, double fo, const double fe[], int n) {ios_base::fmtflags initial;initial = os.setf(ios_base::fixed);//save initial formatting stateos.precision(0);os << "Focal length of objective: " << fo << " mm\n";os.setf(ios::showpoint);os.precision(1);os.width(12);os  << "magnification" << endl;for (int i = 0; i< n; i++) {os.width(12);os << fe[i];os.width(15);os << int (fo/fe[i] + 0.5) << endl;}os.setf(initial);// restore initial formatting state}

0 0