将 cout 的 flag 保存到变量
, 以便修改后的恢复

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

一旦我们使用 boolalpha 将改变 cout 对 bool 值的输出格式. 此后的 cout 都会将 bool 输出为 literals.


将 bool 值以 numeric 输出

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 : 不受影响

如 boolalpha 一样, oct, dec, hex 也是 persistent. 一旦改变, 将影响后续的输出格式.


显示表明 Integer Values 的 Base

cout <<showbase ; // Show base when printing integral values
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/nouppercase

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 6pricision
   控制了至多一共会输出多少个数字. 
   当要输出的数字多余指定的值时, 将发生 四舍五入(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 ; // 输出当前精度

二: 表现形式 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位

恢复到初始状态

cout.unsetf(ostream::floatfield) ; // Retrieve to default handling for notation
cout <<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  


输出填充
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

 

   left to left-justify the output.

cout <<left ; // left-justify
cout <<setw(5) <<12 <<setw(5) <<34 <<endl ; // 12___34___

 

   right to right-justify the output. Output is right-justified bu default.

cout <<right ; // By default
cout <<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___

 

 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___

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)

 

转载于:https://www.cnblogs.com/walfud/articles/2047096.html

cout 格式化输出相关推荐

  1. 有趣的搬砖工 No.2 cout格式化输出

    下面的代码将cout格式化输出的用法都用了一遍. #include <iostream> #include <iomanip>//不要忘记包含此头文件 using namesp ...

  2. c 语言的输出函数cout,详解C++ cout格式化输出完全攻略

    写算法题的时候突然发现自己忘记基本的C++:cout格式化输出了,赶紧拉出以前的C++学习笔记重新看一看. 部分内容来自教程:C语言中文网(一个很棒的网站) 有时希望按照一定的格式进行输出,如按十六进 ...

  3. C++ cout格式化输出,精确控制小数点后位数

    仰天地之正气,法古今之完人. --同济大学老校训 昨天做OJ遇到一题要求把结果保留两位小数输出.惊觉自己完全没有掌握该技能.因此特地去网上搜了一下,发现C++ 的标准输出流的格式化输出很有趣.正好作为 ...

  4. C++ cout格式化输出

    希望按照一定的格式进行输出,如按十六进制输出整数,输出浮点数时保留小数点后面两位,输出整数时按 6 个数字的宽度输出,宽度不足时左边补 0,等等.C++ 中的 cout 对象则使用流操作算子(你也可以 ...

  5. matlab ip 大端,MATLAB格式化输出控制

    MATLAB格式化输出控制 format 默认格式 format short 5字长定点数 format long 15字长定点数 format short e 5字长浮点数 format long ...

  6. C和C++安全编码笔记:格式化输出

    C标准中定义了一些可以接受可变数量参数的格式化输出参数,参数中包括一个格式字符串.printf()和sprintf()都是格式化输出函数的例子.格式化输出函数是由一个格式字符串和可变数目的参数构成的. ...

  7. 【C++grammar】格式化输出与I/O流函数

    目录 1.格式化输出 1. setw manipulator("设置域宽"控制符) 2. setprecision manipulator("设置浮点精度"控制 ...

  8. 【OpenCV】OpenCV函数精讲之 -- 格式化输出方法

    目录 1.OpenCV默认风格输出 2.Python风格输出 3.逗号分隔风格(Comma separated values,CSV) 4.Numpy风格 5.C语言风格 OpenCV提供了风格迥异的 ...

  9. C ++基础 | 格式化输出,文件输入输出(File IO),头文件(Header Files)_3

    目录 格式化输出 文件输入输出(File IO) 头文件(Header Files) 格式化输出 要格式化数据,我们可以使用转义字符串(Escape Sequence)也称字符实体(Character ...

  10. C++基础——格式化输出

    似乎很少有人强调C++的格式化输出的问题,那是因为因为c++的编译器默默地做着许多格式化的动作,以保证C++ Style格式化输出与C-style的格式化的一个根本不同,C++style的格式化输出是 ...

最新文章

  1. 利用Windows自带服务架设免费邮件服务器
  2. scrum项目管理_Scrum,用于初创企业(或针对该项目的任何项目)
  3. linux c 笔记 文件(三)
  4. window.open 打开新窗口被拦截的其他解决方法
  5. Unsupported compiler 'GCC 4.2' selected for architecture 'i386'错误
  6. 002_入门HelloWorldServlet
  7. 请MM吃饭之工厂模式实现
  8. SAP Spartacus 2.1.0 加载homepage的逻辑
  9. matlab改变矩阵的元素,Matlab中元素不变情况下改变矩阵形态——reshape()
  10. 香港中文大学(深圳)吴保元教授课题组博士后招聘
  11. mac os android连接wifi密码,Mac使用小技巧:找回WiFi密码
  12. bat替换文件中的字符串_JavaScript 中替换字符串的几种方法
  13. Dev Express 安装
  14. c语言同余法随机数,线性同余法取随机数
  15. 【Linux operation 09】 - SUSE 12 SP5官网下载
  16. matlab 相机焦距,matlab – 给定焦距和摄像机位置/旋转的正确透视图像
  17. 计算机上机日志如何查找,如何查看金蝶KIS记账王上机日志
  18. 软件测试自学毛笔字纹身,横眉冷对千夫指 俯首甘为孺子牛的毛笔楷书和行书...
  19. html5 移动页面,html5入门到精通,移动设备的html5页面布局
  20. 1 网站压力测试工具 WEB性能测试 Web Bench

热门文章

  1. [IDEA插件] - 一个不错的插件
  2. 转换PHP脚本成为windows的执行程序
  3. python中包引入遇到的问题
  4. 细品慢酌QuickTest关键视图(1)
  5. Hibernate 可编程的配置方式
  6. RSS导入功能已完成
  7. STM32CubeMX使用(六)之RTC及制作时间戳
  8. 【啊哈!算法】算法6:只有五行的Floyd最短路算法
  9. 窗口的新建移动和改变大小
  10. swift学习第四章