#include <iomanip>说明

主要是对cin,cout之类的一些操纵运算子,比如setfill,setw,setbase,setprecision等等。它是I/O流控制头文 件,就像C里面的格式化输出一样.以下是一些常见的控制函数的:

  dec 置基数为10 相当于"%d"

  hex 置基数为16 相当于"%X"

  oct 置基数为8 相当于"%o"

  setfill(c) 设填充字符为c

  setprecision(n) 设显示小数精度为n位

  setw(n) 设域宽为n个字符

  这个控制符的意思是保证输出宽度为n。如:

  cout<<setw(3)<<1<<setw(3)<<10<<setw(3)<<100; 输出结果为

  1 10100 (默认是右对齐)当输出长度大于3时(<<1000),setw(3)不起作用。

  setioflags(ios::fixed) 固定的浮点显示

  setioflags(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 <iostream>

  #include <iomanip>

  using namespace std;

  int main()

  {

  cout<<12345.0<<endl;//输出"12345"

  cout<<setiosflags(ios::fixed)<<setprecision(3)<<1.2345<<endl;输出"1.235"

  cout<<setiosflags(ios::scientific)<<12345.0<<endl;//输出"1.234500e+004 "

  cout<<setprecision(3)<<12345.0<<endl;//输出"1.235e+004 "

  return 0;

  }
以上来自[url]http://www.cppblog.com/masiyou/archive/2009/10/06/97948.aspx[/url]

#include <iostream>
#include <iomanip>

using namespace std;


int main()
{
int x = 15; //Line 1
int y = 7634; //Line 2

cout << "12345678901234567890" << endl; //Line 3
cout << setw(5) << x << setw(7) << y
<< setw(8) << "Warm" << endl; //Line 4

cout << setfill('*'); //Line 5
cout << setw(5) << x << setw(7) << y
<< setw(8) << "Warm" << endl; //Line 6

cout << setw(5) << x << setw(7) << setfill('#')
<< y << setw(8) << "Warm" << endl; //Line 7

cout << setw(5) << setfill('@') << x
<< setw(7) << setfill('#') << y
<< setw(8) << setfill('^') << "Warm"
<< endl; //Line 8

cout << setfill(' '); //Line 9
cout << setw(5) << x << setw(7) << y
<< setw(8) << "Warm" << endl; //Line 10

return 0;
}
输出结果:
12345678901234567890
15 7634 Warm
***15***7634****Warm
***15###7634####Warm
@@@15###7634^^^^Warm
15 7634 Warm
Press any key to continue


很少用的iomanip中地方法:
[table]
| cout << right;
|
| cout << setfill(' ');
|
| cout << setprecision(2); |
[/table]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值