将 cout 的 flag 保存到变量, 以便修改后的恢复
ostream::fmtflags old = cout.flag() ; // 无参将返回当前 flag 值
cout.flag(old) ; // 恢复到原先保存的值
将 bool 值以 literals 输出
cout < span>"numeric : "< span>true< span>" or "< span>false< span>endl ; // 1 or 0
cout < span>"literals : "< span>boolalpha < span>true< span>" or "< span>false< span>endl ; // true or false
cout < span>"literals : "< span>boolalpha < span>0< span>endl ; // 0 原因: 0 在cout中不等价于 false
一旦我们使用 boolalpha 将改变 cout 对 bool 值的输出格式. 此后的 cout 都会将 bool 输出为 literals.
将 bool 值以 numeric 输出
cout < span>"numeric : "< span>noboolalpha < span>true< span>" or "< span>false< span>endl ;// 1 or 0
从此以后, cout 对 bool 值的输出将恢复 numeric 格式
指定 Integral Values 的 Base
const int ival = 17 ; // 'ival' is constant, so value never change
cout < span>"oct : "< span>oct < span>ival < span>endl ; // 21 : 8 进制
cout < span>"dec : "< span>dec < span>ival < span>endl ; // 17 : 10 进制
cout < span>"hex : "< span>hex < span>ival < span>endl ; // 11 : 16 进制
cout < span>"hex : "< span>hex < span>17.01< span>endl ; // 17.01 : 不受影响
显示表明 Integer Values 的 Base
cout < span>showbase ; // Show base when printing integral values
cout < span>"oct : "< span>oct < span>ival < span>endl ; // 21 : 8 进制
cout < span>"dec : "< span>dec < span>ival < span>endl ; // 017 : 10 进制
cout < span>"hex : "< span>hex < span>ival < span>endl ; // 0x11 : 16 进制
cout < span>"hex : "< span>hex < span>17.01< span>endl ; // 17.01 : 不受影响
cout < span>noshowbase ; // Reset state of the stream
若想改变16进制字母的大小, 可以结合 uppercase/nouppercase
cout < span>showbase < span>uppercase ;
cout < span>"hex : "< span>hex < span>15< span>endl ; // 0XF 大写形式
cout < span>nouppercase ;
cout < span>"hex : "< span>hex < span>15< span>endl ; // 0xf 小写形式
showbase 与 noshowbase 的作用周期也是 persistent
对于 float/double 型, 有三种格式化控制
一: 输出精度 precision : by default is 6pricision
控制了至多一共会输出多少个数字.
当要输出的数字多余指定的值时, 将发生 四舍五入(rounded);
当要输出的数字少于指定的值时, 则实际输出的数字个数将少于指定值.
// cout.pricision(4) ; // 等价于 cout < setprecision >
cout < span>setprecision(4) < span>12.345678< span>endl ; // 12.35 rounded!
cout < span>setprecision(10) < span>12.345678< span>endl ; // 12.345678 其实内部发生了 rounded, 而结果正好进位, 与原值相同
cout < span>cout.precision() < span>endl ; // 输出当前精度
二: 表现形式 notation : 'very large and very small values are printed using scientific notation. other values use fixed decimal.'
notation 控制了输出的形式 : 科学计数法(scientific) 和 定点小数(fixed)
恢复到初始状态float f = 101 / 6.0 ;
cout < span>fixed< span>f < span>endl ; // 16.83334 : 小数点后共6位
cout < span>scientific < span>f < span>endl ; // 1.683333e+001 : 小数点后共6位
cout.unsetf(ostream::floatfield) ; // Retrieve to default handling for notation
cout < span>f < span>endl ; // 16.8333 : 所有数字共6位
三: 输出十进制浮点 'By default, when the fractional part of a floating-point value is 0, the decimal point is not displayed. The showpoint manipulator forces the decimal point ot be printed.'
cout < span>10.0< span>endl ; // 10
cout < span>showpoint < span>10.0< span>endl ; // 10.0000
cout < span>noshowpoint < span>endl ; // Revert to default handling of decimal
输出填充 Padding the Output
setw to specify the minimum space for the next numeric or string value.
cout < span>setw(10) < span>12.3< span>endl ; // ______12.3
cout < span>setw(10) < span>12< span>3< span>endl ; // ________123
cout < span>setw(3) < span>12.345< span>endl ; // If the total output is more than 3, it can be extended![]()
left to left-justify the output.
cout < span>left ; // left-justify
cout < span>setw(5) < span>12< span>setw(5) < span>34< span>endl ; // 12___34___![]()
right to right-justify the output. Output is right-justified bu default.
cout < span>right ; // By default
cout < span>setw(5) < span>12< span>setw(5) < span>34< span>endl ; // 12___34___![]()
internal controls placement of the sign on negative value. internal left-justifies the sign and right-justifies the value, padding any intervening space with blanks.(if setfill not set)
cout < span>internal ; // By default
cout < span>setw(5) < -span>12< span>endl ; // 12___34___![]()
setfill lets us specify an alternative character to use when padding the output. By default, the value is a space.
cout < span>setfill('*') ; // By default
cout < span>setw(5) < span>12< span>endl ; // 12___34___
Header Files
Manipulators Defined in < span style='font-size:13px;font-style:normal;font-weight:400;color:#0000ff;' > iomanip</span><
setfill( char ch) Fill whitespace with ' ch '
setprecision( int n) Set floating - point precision to ' n '
setw( int w) Read or write value to ' w ' characters
setbase( int b) Output integers in base ' b ' (only ' b ' is 8 / 10 / 16 could the function work)