现在的位置: 首页 > 综合 > 正文

从零开始学C++之IO流类库(四):输出流格式化(以操纵子方式格式化 以ios类成员函数方式格式化)

2013年09月11日 ⁄ 综合 ⁄ 共 3861字 ⁄ 字号 评论关闭

一、以操纵子方式格式化

数据输入输出的格式控制使用系统头文件<iomanip>中提供的操纵符。把它们作为插入操作符<<的输出对象即可。如setiosflags、setw、setfill、setprecision、hex、oct等。


(一)、常用的流操纵算子:



(二)、ios类的枚举常量



 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <iomanip>

using namespace std;

// 通过操纵子方式进行格式化输出
// 宽度控制
// 对齐控制
// 填充控制
// 精度控制
// 进制输出
int main(void)
{
    //system("chcp 936");
    int n = 64;
    double d = 123.45;
    double d2 = 0.0187;

    cout << "=================宽度控制=====================" << endl;
    cout << n << '#' << endl;
    cout << setw(10) << n << '#' << n << endl;      // 宽度控制不会影响下一个输出

    cout << "=================对齐控制=====================" << endl;
    cout << setw(10) << setiosflags(ios::left) << n << '#' << endl;
    cout << setw(10) << n << '#' << endl;           // 对齐控制会影响下一个输出
    //cout<<setw(10)<<setiosflags(ios::right)<<n<<'#'<<endl;
    cout << setw(10) << resetiosflags(ios::left) << n << '#' << endl; //去除左对齐

    cout << "=================填充控制=====================" << endl;
    cout << setw(10) << setfill('?') << n << '#' << endl;
    cout << setw(10) << n << '#' << endl;           // 填充控制会影响下一个输出
    cout << setw(10) << setfill(' ') << n << '#' << endl;

    cout << "=================精度控制=====================" << endl;
    cout << setprecision(4) << d << endl; //有效数字
    cout << setprecision(2) << d2 << endl;

    cout << setiosflags(ios::fixed);
    cout << setprecision(4) << d << endl; // 小数点后面位数
    cout << setprecision(2) << d2 << endl;

    cout << "=================进制输出=====================" << endl;

    cout << n << endl;
    cout << oct << n << endl;
    cout << hex << n << endl;
    cout << endl;

    cout << setiosflags(ios::showbase); //八进制加前缀0,十六进制加前缀0x
    cout << dec << n << endl;
    cout << oct << n << endl;
    cout << hex << n << endl;

    cout << endl;
    cout << setbase(10) << n << endl; //八进制加前缀0,十六进制加前缀0x
    cout << setbase(8) << n << endl;
    cout << setbase(16) << n << endl;

    return 0;
}



二、以类成员函数方式格式化

通过调用流的成员函数控制格式,如setf、unsetf、width、fill、precision等。优点是在设置格式同时,可以返回以前的设置,便于恢复原来的设置。


ios类提供成员函数对流的状态进行检测和进行输入输出格式控制等操作:




 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
//#include <iomanip>

using namespace std;

// 通过成员函数方式进行格式化输出
// 宽度控制
// 对齐控制
// 填充控制
// 精度控制
// 进制输出
int main(void)
{
    //system("chcp 936");
    int n = 64;
    double d = 123.45;
    double d2 = 0.0187;

    cout << "=================宽度控制=====================" << endl;
    cout << n << '#' << endl;
    cout.width(10);
    cout << n << '#' << n << endl;                  // 宽度控制不会影响下一个输出

    cout << "=================对齐控制=====================" << endl;
    cout.width(10);
    cout.setf(ios::left);
    cout << n << '#' << endl;
    cout.width(10);
    cout << n << '#' << endl;               // 对齐控制会影响下一个输出
    //cout.width(10);
    //cout.setf(ios::right);
    //cout<<n<<'#'<<endl;

    cout.width(10);
    cout.unsetf(ios::left);
    cout << n << '#' << endl;

    cout << "=================填充控制=====================" << endl;
    cout.width(10);
    cout.fill('?');
    cout << n << '#' << endl;

    cout.width(10);
    cout << n << '#' << endl;               // 填充控制会影响下一个输出

    cout.width(10);
    cout.fill(' ');
    cout << n << '#' << endl;

    cout << "=================精度控制=====================" << endl;
    cout.precision(4);
    cout << d << endl;
    cout.precision(2);
    cout << d2 << endl;

    cout.setf(ios::fixed);
    cout.precision(4);
    cout << d << endl;
    cout.precision(2);
    cout << d2 << endl;;

    cout << "=================进制输出=====================" << endl;

    cout.setf(ios::showbase);
    cout << n << endl;
    cout.unsetf(ios::dec);
    cout.setf(ios::oct);
    cout << n << endl;

    cout.unsetf(ios::oct);
    cout.setf(ios::hex);
    cout << n << endl;

    return 0;
}



参考:

C++ primer 第四版
Effective C++ 3rd
C++编程规范


抱歉!评论已关闭.