您的位置:首页 > 其它

格式输出iomanip的几个函数

2012-10-02 11:03 134 查看
C语言的格式输出用scanf可以直接控制,C++中是通过一些库函数来实现。

setw()设定输出字段的最小宽度,setfill()设定默认填充的字符,默认是空格。

 

DEMO

 

#include <iomanip>

#include <iostream>

#include <ostream>

using namespace std;

int main()

{

 

 int const low(1);

 int const high(15);

 int const colwidth(8);

 cout<<right;

 cout<<setw(colwidth)<<"*"<<"|";

 for(int i(low);i <= high; i++)

  cout<<setw(colwidth)<<i;

 cout<<endl;

 cout<<setfill('-')<<setw(colwidth)<<""<<'+'<<setw((high-low+1)*colwidth)<<""<<"\n";

 cout<<setfill(' ');

 for(int row(low); row <= high; row++)

 {

  cout<<setw(colwidth)<<row<<"|";

  for(int col(low); col <= high; col++)

   cout<<setw(colwidth)<<row*col;

  cout<<endl;

 }

 return 0;

}

 

OUPT

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  语言 c