C++ 标准库之iomanip

来源:互联网 发布:淘宝富安娜蚕丝被 编辑:程序博客网 时间:2024/06/07 06:29

C++ 语言下
头文件:#include <iomanip>
说明:是I/O流控制头文件,就像C里面的格式化输出一样

         控   制   符                            作           用                                      dec  设置整数为十进制 hex 设置整数为十六进制 oct 设置整数为八进制 setbase(n) 设置整数为n进制(n=8,10,16) setfill(n)

 设置字符填充,c可以是字符常或符变量

 setprecision(n) 设置浮点数的有效数字为n位 setw(n) 设置字段宽度为n位 setiosflags(ios::fixed) 设置浮点数以固定的小数位数显示 setiosflags(ios::scientific)   设置浮点数以科学计数法表示 setiosflags(ios::left) 输出左对齐 setiosflags(ios::right) 输出右对齐 setiosflags(ios::skipws) 忽略前导空格 setiosflags(ios::uppercase) 在以科学计数法输出E与十六进制输出X以大写输出,否则小写。 setiosflags(ios::showpos) 输出正数时显示"+"号 setiosflags(ios::showpoint) 强制显示小数点 resetiosflags() 

 终止已经设置的输出格式状态,在括号中应指定内容

在此需要说一下,有效位数默认是6位,即setprecision(6),即小数点前面和小数点后面加起来的位数为6个有效数字(注意会四舍五入)。
另外,科学计数法输出E与十六进制输出默认是以小写的,要换成大写需添加uppercase
而setw(n)设置宽度,若是实际宽度大于被设置的,则setw函数此时失效。

以下是测试程序:

[cpp] view plain copy
print?
  1. #include <iostream>  
  2. #include <iomanip>      
  3. using namespace std ;  
  4. int main()  
  5. {  
  6.     double PI=3.141592654;  
  7.     cout<<PI<<endl;  
  8.     cout<<setprecision(2)<<PI<<endl;  
  9.     cout<<fixed<<setprecision(2)<<PI<<endl;   
  10.     cout<<setfill('*')<<setw(20)<<setprecision(10)<<PI<<endl;  
  11.     cout<<setfill('*')<<setw(20)<<setprecision(10)<<left<<PI<<endl;  
  12.     cout<<scientific<<setprecision(10)<<PI<<endl;  
  13.     cout<<scientific<<uppercase<<setprecision(10)<<PI<<endl;    
  14.     return 0 ;  
  15. }  

结果如下:
 

相关课件,更多的内容在课件里面,收集于网络:下载一

以下是<iomanip>头文件的声明:

[cpp] view plain copy
print?
  1. // iomanip standard header  
  2. #pragma once  
  3. #ifndef _IOMANIP_  
  4. #define _IOMANIP_  
  5. #ifndef RC_INVOKED  
  6. #include <istream>  
  7. #ifdef _MSC_VER  
  8.  #pragma pack(push,_CRT_PACKING)  
  9.  #pragma warning(push,3)  
  10. #endif  /* _MSC_VER */  
  11. _STD_BEGIN  
  12.         // TEMPLATE STRUCT _Fillobj  
  13. template<class _Elem>  
  14.     struct _Fillobj  
  15.     {   // store fill character  
  16.     _Fillobj(_Elem _Ch)  
  17.         : _Fill(_Ch)  
  18.         {   // construct from fill character  
  19.         }  
  20.     _Elem _Fill;    // the fill character  
  21.     };  
  22.         // TEMPLATE FUNCTION setfill  
  23. template<class _Elem> inline  
  24.     _Fillobj<_Elem> __CLRCALL_OR_CDECL setfill(_Elem _Ch)  
  25.     {   // return a _Fillobj manipulator  
  26.     return (_Fillobj<_Elem>(_Ch));  
  27.     }  
  28. template<class _Elem,  
  29.     class _Traits> inline  
  30.     basic_istream<_Elem, _Traits>&  
  31.         __CLRCALL_OR_CDECL operator>>(basic_istream<_Elem, _Traits>& _Istr,  
  32.             const _Fillobj<_Elem>& _Manip)  
  33.     {   // set fill character in input stream  
  34.     _Istr.fill(_Manip._Fill);  
  35.     return (_Istr);  
  36.     }  
  37. template<class _Elem,  
  38.     class _Traits> inline  
  39.     basic_ostream<_Elem, _Traits>&  
  40.         __CLRCALL_OR_CDECL operator<<(basic_ostream<_Elem, _Traits>& _Ostr,  
  41.             const _Fillobj<_Elem>& _Manip)  
  42.     {   // set fill character in output stream  
  43.     _Ostr.fill(_Manip._Fill);  
  44.     return (_Ostr);  
  45.     }  
  46.         // TEMPLATE STRUCT _Smanip  
  47. template<class _Arg>  
  48.     struct _Smanip  
  49.     {   // store function pointer and argument value  
  50.     _Smanip(void (__cdecl *_Left)(ios_base&, _Arg), _Arg _Val)  
  51.         : _Pfun(_Left), _Manarg(_Val)  
  52.         {   // construct from function pointer and argument value  
  53.         }  
  54.     void (__cdecl *_Pfun)(ios_base&, _Arg); // the function pointer  
  55.     _Arg _Manarg;   // the argument value  
  56.     };  
  57. template<class _Elem,  
  58.     class _Traits,  
  59.     class _Arg> inline  
  60.     basic_istream<_Elem, _Traits>& __CLRCALL_OR_CDECL operator>>(  
  61.         basic_istream<_Elem, _Traits>& _Istr, const _Smanip<_Arg>& _Manip)  
  62.     {   // extract by calling function with input stream and argument  
  63.     (*_Manip._Pfun)(_Istr, _Manip._Manarg);  
  64.     return (_Istr);  
  65.     }  
  66. template<class _Elem,  
  67.     class _Traits,  
  68.     class _Arg> inline  
  69.     basic_ostream<_Elem, _Traits>& __CLRCALL_OR_CDECL operator<<(  
  70.         basic_ostream<_Elem, _Traits>& _Ostr, const _Smanip<_Arg>& _Manip)  
  71.     {   // insert by calling function with output stream and argument  
  72.     (*_Manip._Pfun)(_Ostr, _Manip._Manarg);  
  73.     return (_Ostr);  
  74.     }  
  75.         // INSTANTIATIONS  
  76. _MRTIMP2 _Smanip<ios_base::fmtflags> __cdecl resetiosflags(ios_base::fmtflags);  
  77. _MRTIMP2 _Smanip<ios_base::fmtflags> __cdecl setiosflags(ios_base::fmtflags);  
  78. _MRTIMP2 _Smanip<int> __cdecl setbase(int);  
  79. _MRTIMP2 _Smanip<streamsize> __cdecl setprecision(streamsize);  
  80. _MRTIMP2 _Smanip<streamsize> __cdecl setw(streamsize);  
  81. _STD_END  
  82. #ifdef _MSC_VER  
  83.  #pragma warning(pop)  
  84.  #pragma pack(pop)  
  85. #endif  /* _MSC_VER */  
  86. #endif /* RC_INVOKED */  
  87. #endif /* _IOMANIP_ */  
  88. /* 
  89.  * Copyright (c) 1992-2006 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  90.  * Consult your license regarding permissions and restrictions. 
  91.  V5.02:0009 */  

 

原创粉丝点击