1: 每个iostream对象维持一个控制IO格式化细节的格式状态。标准库定义了一组操纵符来修改对象的格式状态。所谓操纵符是可用作输入或输出操作符的函数或对象。iostream和iomanip头文件中分别定义了一些操纵符,如表所示:
2:
3: iostream中定义的操纵符
4:
5: boolalpha 将真和假显示为字符串
6: * noboolalpha 将真和假显示为1, 0
7: showbase 产生指出数的基数的前缀
8: * noshowbase 不产生记数基数前缀
9: showpoint 总是显示小数点
10: * noshowbase 有小数部分才显示小数点
11: showpos 显示非负数中的+
12: * noshowpos 不显示非负数中的+
13: uppercase 在十六进制中打印0X,科学记数法中打印E
14: * nouppercase 在十六进制中打印0x,科学记数法中打印e
15: * dec 用十进制显示
16: hex 用十六进制显示
17: oct 用八进制显示
18: left 在值的右边增加填充字符
19: right 在值的左边增加填充字符
20: internal 在符号和值之间增加填充字符
21: fixed 用小数形式显示浮点数
22: scientific 用科学记数法显示浮点数
23: flush 刷新ostream缓冲区
24: ends 插入空字符,然后刷新ostream缓冲区
25: endl 插入换行符,然后刷新ostream缓冲区
26: unitbuf 在每个输出操作之后刷新缓冲区
27: * nounitbuf 恢复常规缓冲区刷新
28: * skipws 不为输入操作符跳过空白
29: ws “吃掉”空白
30:
31: 注:带*的是默认状态。
32:
33: iomanip中定义的操纵符
34:
35: setfill(ch) 用ch填充空白
36: setprecision(n) 将浮点精度置为n
37: setw(w) 读写w个字符的值
38: setbase(b) 按基数b输出整数
39:
40: 可以使用flags操作符恢复格式状态
41:
42: 1、不带实参的flags()返回流的当前格式状态。返回值是名为fmtflags的标准库定义类型。
43:
44: 2、flags(arg)接受一个实参并将流格式置为实参所指定的格式。
45:
46: Sample:
47:
48: void display(ostream& os)
49: {
50: //remember the current format state
51: ostream::fmtflags curr_fmt = os.flags();
52: //do output that uses manipulators that change the format state of os
53: os.flags(curr_fmt); //restore the original format state of os
54: }
55:
56:
57:
58:
59:
60: 使用举例:
61:
62: 1、控制布尔值 boolalpha:
63: bool flag = flase;
64:
65: cout << flag << endl; //Display: 0
66:
67: cout << boolalpha << flag << endl; //Display: false
68:
69: 2、控制整形 oct、hex:
70:
71: int ival = 16;
72:
73: cout << ival << endl; //Display: 16
74:
75: cout << oct << ival << endl; //Display: 20
76:
77: cout << hex << ival << endl; //Display: 10
78:
79: 指出输出的基数及形式 showbase:
80:
81: cout << showbase; //Set format that showbase
82: cout << ival << endl; //Display: 16
83: cout << oct << ival << endl; //Display: 020
84: cout << hex << ival << endl; //Display: 0x10
85:
86: cout << uppercase << hex << ival << endl; //Display:0X10
87:
88: cout << noshowbase; //Set the default format
89:
90: 3、指定显示精度 precision setprecision
91:
92: 默认情况下,精度控制显示的数字总位数。显示的时候,将浮点值四舍五入到当前精度。因此,如果精度为4,则3.1415926成为3.142。
93:
94: Sample:
95:
96: float pi = 3.1415926535;
97: //cout.precision reports current precision value
98: cout << cout.precision() << " " << pi << endl; //Display: 6 3.14159
99:
100: //one way to set format
101: cout.precision(8);
102: cout << pi << endl; //Display: 3.1415927
103: //another way to set format
104: cout << setprecision(3) << pi << endl; //Display: 3.14
105:
106: 4、控制浮点数的记数法
107:
108: 默认情况下,用于显示浮点值的记数法取决于数的大小,如果数很大或很小,将按科学记数法显示,否则,使用固定位数的小数。可以使用scientific或fixed来修改浮点数的默认记数法。但是要恢复默认记数法则必须调用unsetf成员来取消它们所做的改变。
109:
110: cout.unsetf(ofstream::floatfield); //reset to default handling for notation
111:
112: Sample:
113:
114: cout << sqrt(2.0) << endl; //Dispaly: 1.41421
115: cout << scientific << sqrt(2.0) << endl; //Display: 1.414214e+00
116: cout << fixed << sqrt(2.0) << endl; //Display: 1.414214
117: cout.unsetf(ostream::floatfield);
118:
119: 5、填充输出 setw left right internal setfill:
120:
121: int i = -6;
122: double d = 3.14159;
123: cout << setw(12) << i << endl;
124: cout << setw(12) << d << endl;
125: cout << left << setw(12) << i << endl;
126: cout << setw(12) << d << endl;
127: cout << right;
128: cout << internal << setw(12) << i << endl;
129: cout << setw(12) << d << endl;
130: cout << setfill('$') << setw(12) << i << endl;
131: cout << setw(12) << d << endl;
132: cout << setfill(' ');
133:
134: Display:
135:
136:
137:
138: 5、控制输入格式化
139:
140: 默认情况下, 输入操作符忽略空白。如:
141:
142: while(cin >> ch)
143: cout << ch;
144:
145: 给定输入序列:
146:
147: a b c(分别为一个空格和两个空格)
148:
149: d
150:
151: 循环执行四次从字符a读到d,跳过介于其间的空白。所以输出abcd.
152:
153: 如果是:
154:
155: cin >> noskipws;
156: while(cin >> ch)
157: cout << ch;
158: cin >> skipws;
159:
160: 则同样的输入将输出:
161:
162: a b c
163:
164: d
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }