cout 格式化输出
来源:互联网 发布:php无数据库留言板源码 编辑:程序博客网 时间:2024/06/06 12:48
将 cout 的 flag 保存到变量, 以便修改后的恢复
- ostream::fmtflags old = cout.flag() ; // 无参将返回当前 flag 值
- cout.flag(old) ; // 恢复到原先保存的值
ostream::fmtflags old = cout.flag() ; // 无参将返回当前 flag 值cout.flag(old) ; // 恢复到原先保存的值
将 bool 值以 literals 输出
- cout <<“numeric : ” <<true <<“ or ” <<false <<endl ; // 1 or 0
- cout <<”literals : ” <<boolalpha <<true <<“ or ” <<false <<endl ; // true or false
- cout <<”literals : ” <<boolalpha <<0 <<endl ; // 0 原因: 0 在cout中不等价于 false
cout <<"numeric : " <<true <<" or " <<false <<endl ; // 1 or 0cout <<"literals : " <<boolalpha <<true <<" or " <<false <<endl ; // true or falsecout <<"literals : " <<boolalpha <<0 <<endl ; // 0 原因: 0 在cout中不等价于 false一旦我们使用 boolalpha 将改变 cout 对 bool 值的输出格式. 此后的 cout 都会将 bool 输出为 literals.
将 bool 值以 numeric 输出
- cout <<“numeric : ” <<noboolalpha <<true <<“ or ” <<false <<endl ;// 1 or 0
cout <<"numeric : " <<noboolalpha <<true <<" or " <<false <<endl ;// 1 or 0从此以后, cout 对 bool 值的输出将恢复 numeric 格式
指定 Integral Values 的 Base(进制)
- const int ival = 17 ; // ‘ival’ is constant, so value never change
- cout <<”oct : ” <<oct <<ival <<endl ; // 21 : 8 进制
- cout <<”dec : ” <<dec <<ival <<endl ; // 17 : 10 进制
- cout <<”hex : ” <<hex <<ival <<endl ; // 11 : 16 进制
- cout <<”hex : ” <<hex <<17.01 <<endl ; // 17.01 : 不受影响
const int ival = 17 ; // 'ival' is constant, so value never changecout <<"oct : " <<oct <<ival <<endl ; // 21 : 8 进制cout <<"dec : " <<dec <<ival <<endl ; // 17 : 10 进制cout <<"hex : " <<hex <<ival <<endl ; // 11 : 16 进制cout <<"hex : " <<hex <<17.01 <<endl ; // 17.01 : 不受影响如 boolalpha 一样, oct, dec, hex 也是 persistent(持续的). 一旦改变, 将影响后续的输出格式.
显示表明 Integer Values 的 Base
- cout <<showbase ; //打印整数值时显示进制
- cout <<”oct : ” <<oct <<ival <<endl ; // 21 : 8 进制
- cout <<”dec : ” <<dec <<ival <<endl ; // 017 : 10 进制
- cout <<”hex : ” <<hex <<ival <<endl ; // 0x11 : 16 进制
- cout <<”hex : ” <<hex <<17.01 <<endl ; // 17.01 : 不受影响
- cout <<noshowbase ; // Reset state of the stream
cout <<showbase ; //打印整数值时显示进制cout <<"oct : " <<oct <<ival <<endl ; // 21 : 8 进制cout <<"dec : " <<dec <<ival <<endl ; // 017 : 10 进制cout <<"hex : " <<hex <<ival <<endl ; // 0x11 : 16 进制cout <<"hex : " <<hex <<17.01 <<endl ; // 17.01 : 不受影响cout <<noshowbase ; // Reset state of the stream若想改变16进制字母的大小, 可以结合 uppercase(大写显示进制,如0X)/nouppercase(小写显示进制,如0x)
- cout <<showbase <<uppercase ;
- cout <<”hex : ” <<hex <<15 <<endl ; // 0XF 大写形式
- cout <<nouppercase ;
- cout <<”hex : ” <<hex <<15 <<endl ; // 0xf 小写形式
cout <<showbase <<uppercase ;cout <<"hex : " <<hex <<15 <<endl ; // 0XF 大写形式cout <<nouppercase ;cout <<"hex : " <<hex <<15 <<endl ; // 0xf 小写形式showbase 与 noshowbase 的作用周期也是 persistent
对于 float/double 型, 有三种格式化控制
一: 输出精度 precision : by default is 6 pricision
控制了至多一共会输出多少个数字.
当要输出的数字多余指定的值时, 将发生 四舍五入(rounded);
当要输出的数字少于指定的值时, 则实际输出的数字个数将少于指定值.
- // cout.pricision(4) ; // 等价于 cout <<setprecision(4) ;
- cout <<setprecision(4) <<12.345678 <<endl ; // 12.35 rounded!
- cout <<setprecision(10) <<12.345678 <<endl ; // 12.345678 其实内部发生了 rounded, 而结果正好进位, 与原值相同
- cout <<cout.precision() <<endl ; // 输出当前精度
// cout.pricision(4) ; // 等价于 cout <<setprecision(4) ;cout <<setprecision(4) <<12.345678 <<endl ; // 12.35 rounded!cout <<setprecision(10) <<12.345678 <<endl ; // 12.345678 其实内部发生了 rounded, 而结果正好进位, 与原值相同cout <<cout.precision() <<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 <<fixed <<f <<endl ; // 16.83334 : 小数点后共6位
- cout <<scientific <<f <<endl ; // 1.683333e+001 : 小数点后共6位
float f = 101 / 6.0 ;cout <<fixed <<f <<endl ; // 16.83334 : 小数点后共6位cout <<scientific <<f <<endl ; // 1.683333e+001 : 小数点后共6位恢复到初始状态
- cout.unsetf(ostream::floatfield) ; // Retrieve to default handling for notation
- cout <<f <<endl ; // 16.8333 : 所有数字共6位
cout.unsetf(ostream::floatfield) ; // Retrieve to default handling for notationcout <<f <<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 <<10.0 <<endl ; // 10
- cout <<showpoint <<10.0 <<endl ; // 10.0000
- cout <<noshowpoint <<endl ; // Revert to default handling of decimal
cout <<10.0 <<endl ; // 10cout <<showpoint <<10.0 <<endl ; // 10.0000cout <<noshowpoint <<endl ; // Revert to default handling of decimal
输出填充 Padding the Output
setw to specify the minimum space for the next numeric or string value.
- cout <<setw(10) <<12.3 <<endl ; // ______12.3
- cout <<setw(10) <<12 <<3 <<endl ; // ________123
- cout <<setw(3) <<12.345 <<endl ; // If the total output is more than 3, it can be extended
cout <<setw(10) <<12.3 <<endl ; // ______12.3cout <<setw(10) <<12 <<3 <<endl ; // ________123cout <<setw(3) <<12.345 <<endl ; // If the total output is more than 3, it can be extended
left to left-justify the output.
- cout <<left ; // left-justify
- cout <<setw(5) <<12 <<setw(5) <<34 <<endl ; // 12___34___
cout <<left ; // left-justifycout <<setw(5) <<12 <<setw(5) <<34 <<endl ; // 12___34___
- cout <<right ; // By default
- cout <<setw(5) <<12 <<setw(5) <<34 <<endl ; // 12___34___
cout <<right ; // By defaultcout <<setw(5) <<12 <<setw(5) <<34 <<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 <<internal ; // By default
- cout <<setw(5) <<-12 <<endl ; // 12___34___
cout <<internal ; // By defaultcout <<setw(5) <<-12 <<endl ; // 12___34___
setfill lets us specify an alternative character to use when padding the output. By default, the value is a space.
- cout <<setfill(‘*’) ; // By default
- cout <<setw(5) <<12 <<endl ; // 12___34___
cout <<setfill('*') ; // By defaultcout <<setw(5) <<12 <<endl ; // 12___34___
Header Files
- Manipulators Defined in <iomanip>
- 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)
Manipulators Defined in <iomanip>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' characterssetbase(int b) Output integers in base 'b'(only 'b' is 8/10/16 could the function work)
【转自:http://blog.csdn.net/yockie/article/details/9104899】
阅读全文
0 0
- cout 格式化输出
- cout格式化输出示例
- cout格式化输出
- cout格式化输出 详解
- cout 格式化输出
- cout 格式化输出
- cout 格式化输出
- cout的格式化输出
- cout 格式化输出详解
- cout 格式化输出详解
- cout的格式化输出
- cout 格式化输出
- cout格式化输出
- cout格式化输出
- cout 格式化输出
- cout 格式化输出
- C++ cout格式化输出
- cout格式化输出
- 7-5 银行业务队列简单模拟(25 point(s))
- 你可能不知道的javascript调试技巧
- 网址
- Java for Web学习笔记(八七):消息和集群(2)应用内的publish和subscribe
- MySQL
- cout 格式化输出
- Leetcode:111. Minimum Depth of Binary Tree(求Tree的最小的深度)
- 7-7 是否同一棵二叉搜索树(25 point(s))
- Pearson相关系数公式的四种形式及Python代码实现
- 递归-打印整数
- 数据结构实验之二叉树七:叶子问题
- Unable to update database to match the current model!
- java.lang.NoSuchMethodError: org.springframework.core.GenericTypeResolver.resolveTypeArguments(Ljava
- DP? HDU