(转)百度百科-iomanip.h

来源:互联网 发布:authorware是什么软件 编辑:程序博客网 时间:2024/05/20 06:52
iomanip.h是I/O流控制头文件,就像C里面的格式化输出一样.
  在新版本的c++中头文件已经用iomanip取代了iomanip.h。
  以下是一些常用的函数:
  dec 置基数为10 相当于"%d"
  hex 置基数为16 相当于"%X"
  oct 置基数为8 相当于"%o"
  setfill(c) 设填充字符为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) 16进制数大写输出
  setiosflags(ios::lowercase) 16进制小写输出
  setiosflags(ios::showpoint) 强制显示小数点
  setiosflags(ios::showpos) 强制显示符号
  示例:
  #include <iomanip.h>
  #include <iostream>
  using namespace std;
  int main()
  { cout<<12345.0<<endl;//12345
  cout<<setiosflags(ios::scientific)<<12345.0<<endl;//1.234500e+004
  cout<<setprecision(3)<<12345.0<<endl;//1.23e+004

  return 0; }


//程序员面试宝典const题3

#include <iostream>
#include <iomanip>


using namespace std;


class C
{
public:
        C(int i):m_Count(i){}


        int incr()const
        {
                return ++m_Count;
        }


        int decr()const
        {
                return --m_Count;
        }
private:
        mutable  int m_Count;
};


int main(void)
{
        C c1(0),c2(10);
        for(int tmp,i = 0; i < 10; i++)
        {
                tmp = c1.incr();
//              cout << setw(tmp) << setfill(' ') << tmp << endl;
                cout << setw(tmp) << tmp << endl;
                tmp = c2.decr();
                cout << setw(tmp) << setfill(' ') << tmp << endl;
        }
        return 0;
}


结果:

1
        9
 2
       8
  3
      7
   4
     6
    5
    5
     6
   4
      7
  3
       8
 2
        9
1
        10
0


原创粉丝点击