# Good Good code,Day Day up

PearLi's Blog

• 随笔 - 50
• 文章 - 42
• 评论 - 147
• 引用 - 0

•

• 积分 - 155502
• 排名 - 161

### 评论排行榜

#### A.3.3. 控制输出格式

##### 控制布尔值和格式

`cout << "default bool values: "          << true << " " << false          << "\nalpha bool values: "          << boolalpha          << true << " " << false          << endl;`

`default bool values: 1 0     alpha bool values: true false`

`bool bool_val;cout << boolalpha    // sets internal state of cout     << bool_val     << noboolalpha; // resets internal state to default formatting`

##### 指定整型值的基数

`const int ival = 15, jval = 1024; // const, so values never change     cout << "default: ival = " << ival          << " jval = " << jval << endl;     cout << "printed in octal: ival = " << oct << ival          << " jval = " << jval << endl;     cout << "printed in hexadecimal: ival = " << hex << ival          << " jval = " << jval << endl;     cout << "printed in decimal: ival = " << dec << ival          << " jval = " << jval << endl;`

`default: ival = 15 jval = 1024     printed in octal: ival = 17 jval = 2000     printed in hexadecimal: ival = f jval = 400     printed in decimal: ival = 15 jval = 1024`

##### 指出输出的基数

• 以 0x 为前导表示十六进制。

• 以 0 为前导表示八进制。

• 没有任何前导表示十进制。

`const int ival = 15, jval = 1024; // const so values never change     cout << showbase; // show base when printing integral values     cout << "default: ival = " << ival          << " jval = " << jval << endl;     cout << "printed in octal: ival = " << oct << ival          << " jval = " << jval << endl;     cout << "printed in hexadecimal: ival = " << hex << ival          << " jval = " << jval << endl;     cout << "printed in decimal: ival = " << dec << ival          << " jval = " << jval << endl;     cout << noshowbase; // reset state of the stream`

`default: ival = 15 jval = 1024     printed in octal: ival = 017 jval = 02000     printed in hexadecimal: ival = 0xf jval = 0x400     printed in decimal: ival = 15 jval = 1024`

noshowbase 操纵符重置 cout，以便它不再显示整型值的表示基数。

`cout << uppercase << showbase << hex          << "printed in hexadecimal: ival = " << ival          << " jval = " << jval << endl          << nouppercase << endl;`

`printed in hexadecimal: ival = 0XF jval = 0X400`

##### 控制浮点值的格式

• 精度：显示多少位数字。

• 记数法：用小数还是科学记法法显示。

• 对是整数的浮点值的小数点的处理。

##### 指定显示精度

`// cout.precision reports current precision value     cout << "Precision: " << cout.precision()          << ", Value: "   << sqrt(2.0) << endl;     // cout.precision(12) asks that 12 digits of precision to be printed     cout.precision(12);     cout << "Precision: " << cout.precision()          << ", Value: "   << sqrt(2.0) << endl;     // alternative way to set precision using setprecision manipulator     cout << setprecision(3);     cout << "Precision: " << cout.precision()          << ", Value: "   << sqrt(2.0) << endl;`

`Precision: 6, Value: 1.41421     Precision: 12, Value: 1.41421356237     Precision: 3, Value: 1.41`

 操纵符和其他接受实参的操纵符定义在头文件 iomanip 中。

##### 控制记数法

 将浮点数显示为普通数（相对于显示货币、百分比，那时我们希望控制值的外观）的时候，通常最好让标准库来选择使用的记数法。要强制科学记数法或固定位数小数的一种情况是在显示表的时候，表中的小数点应该对齐。

##### 恢复浮点值的默认记数法

`// reset to default handling for notation     cout.unsetf(ostream::floatfield);`

`cout << sqrt(2.0) << '\n' << endl;     cout << "scientific: " << scientific << sqrt(2.0) << '\n'          << "fixed decimal: " << fixed << sqrt(2.0) << "\n\n";     cout << uppercase          << "scientific: " << scientific << sqrt(2.0) << '\n'          << "fixed decimal: " << fixed << sqrt(2.0) << endl          << nouppercase;     // reset to default handling for notation     cout.unsetf(ostream::floatfield);     cout << '\n' << sqrt(2.0) << endl;`

`1.41421     scientific: 1.414214e+00     fixed decimal: 1.414214     scientific: 1.414214E+00     fixed decimal: 1.414214     1.41421`
##### 显示小数点

`cout << 10.0 << endl;        // prints 10     cout << showpoint << 10.0    // prints 10.0000          << noshowpoint << endl; // revert to default handling of decimal point`

noshowpoint 操纵符恢复默认行为。下一个输出表达式将具有默认行为，即，如果浮点值小数部分为 0,就取消小数点。

##### 填充输出

• setw，指定下一个数值或字符串的最小间隔。

• left，左对齐输出。

right，右对齐输出。输出默认为右对齐。

• internal，控制负值的符号位置。internal 左对齐符号且右对齐值，用空格填充介于其间的空间。

• setfill，使我们能够指定填充输出时使用的另一个字符。默认情况下，值是空格。

 像 endl 一样，setw 不改变输出流的内部状态，它只决定下一个输出的长度。

`int i = -16;     double d = 3.14159;     // pad first column to use minimum of 12 positions in the output     cout << "i: " << setw(12) << i << "next col" << '\n'          << "d: " << setw(12) << d << "next col" << '\n';     // pad first column and left-justify all columns     cout << left          << "i: " << setw(12) << i << "next col" << '\n'          << "d: " << setw(12) << d << "next col" << '\n'          << right; // restore normal justification     // pad first column and right-justify all columns     cout << right          << "i: " << setw(12) << i << "next col" << '\n'          << "d: " << setw(12) << d << "next col" << '\n';     // pad first column but put the padding internal to the field     cout << internal          << "i: " << setw(12) << i << "next col" << '\n'          << "d: " << setw(12) << d << "next col" << '\n';     // pad first column, using # as the pad character     cout << setfill('#')          << "i: " << setw(12) << i << "next col" << '\n'          << "d: " << setw(12) << d << "next col" << '\n'          << setfill(' '); // restore normal pad character`

`i:          -16next col     d:      3.14159next col     i: -16         next col     d: 3.14159     next col     i:          -16next col     d:      3.14159next col     i: -         16next col     d:      3.14159next col     i: -#########16next col     d: #####3.14159next col`

#### A.3.4. 控制输入格式化

`while (cin >> ch)         cout << ch;`

`a b   c     d`

`abcd`

noskipws 操纵符导致输入操作符读（而不是跳过）空白。要返回默认行为，应用 skipws 操纵符：

`cin >> noskipws;      // set cin so that it reads whitespace     while (cin >> ch)             cout << ch;     cin >> skipws; // reset cin to default state so that it discards whitespace`

`a b    c     d`

#### A.3.5. 未格式化的输入／输出操作

posted on 2009-05-27 17:53 pear_li 阅读(1154) 评论(0)  编辑 收藏 引用 所属分类: C++