C++ std cout格式化输出

栏目: C++ · 发布时间: 7年前

点击( 此处 )折叠或打开

  1. // 方法1
  2. cout < < hex < < i < < endl ; //输出十六进制数
  3. cout < < oct < < i < < endl ; //输出八进制数
  4. cout < < dec < < i < < endl ; //输出十进制数
  5. //方法2
  6. // 输出16进制, setbase(int)可以设置8等。
  7.  cout < < setbase ( 16 ) < < i < < endl ;
  8. // 方法3
  9.  setiosflags ( ios : : showbase ) ;
  10.  cout < < hex < < i < < endl ;
  11. cout . setf ( ios : : showbase ) ;
  12.  cout . setf ( ios_base : : hex , ios_base : : basefield )

点击( 此处 )折叠或打开

  1. cout 输出 16 , 8 , 2进制
  2. # include < iostream >
  3. # include < iomanip >
  4. # include < bitset >
  5. using std : : bitset ;
  6. using std : : hex ;
  7. using std : : oct ;
  8. using std : : cout ;
  9. using std : : cin ;
  10. using std : : endl ;
  11. int main ( )
  12. {
  13.   int a = 10 ;
  14.  cout < < "Dec:" < < a < < endl ;
  15.  cout < < hex < < "Hex:" < < a < < endl ;
  16.  cout < < oct < < "Oct:" < < a < < endl ;
  17.  cout < < bitset < 32 > ( a ) < < endl ;
  18.  getchar ( ) ;
  19.  return 0 ;

点击( 此处 )折叠或打开

  1. C + + 操作符
  2.  
  3. 注:下面的scientific 和 fixed不能同时使用
  4. double a = 123 . 456789012345 ; 对a赋初值
  5. ( 1 ) cout < < a ; 输出: 123 . 456
  6. ( 2 ) cout < < setprecision ( 9 ) < < a ; 输出: 123 . 456789
  7. ( 3 ) cout < < setprecision ( 6 ) ; 恢复默认格式 ( 精度为6 )
  8. ( 4 ) cout < < setiosflags ( ios∷fixed ) ; 输出: 123 . 456789
  9. ( 5 ) cout < < setiosflags ( ios∷fixed ) < < setprecision ( 8 ) < < a ; 输出: 123 . 45678901
  10. ( 6 ) cout < < setiosflags ( ios∷scientific ) < < a ; 输出: 1 . 234568e + 02
  11. ( 7 ) cout < < setiosflags ( ios∷scientific ) < < setprecision ( 4 ) < < a ; 输出: 1 . 2346e02
  12. 下面是整数输出的例子:
  13. int b = 123456 ; 对b赋初值
  14. ( 1 ) cout < < b ; 输出: 123456
  15. ( 2 ) cout < < hex < < b ; 输出: 1e240
  16. ( 3 ) cout < < setiosflags ( ios∷uppercase ) < < b ; 输出: 1E240
  17. ( 4 ) cout < < setw ( 10 ) < < b < <,< < b ; 输出: 123456,123456
  18. ( 5 ) cout < < setfill (*) < < setw ( 10 ) < < b ; 输出: * * * * 123456
  19. ( 6 ) cout < < setiosflags ( ios∷showpos ) < < b ; 输出: + 123456

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Ant Colony Optimization

Ant Colony Optimization

Marco Dorigo、Thomas Stützle / A Bradford Book / 2004-6-4 / USD 45.00

The complex social behaviors of ants have been much studied by science, and computer scientists are now finding that these behavior patterns can provide models for solving difficult combinatorial opti......一起来看看 《Ant Colony Optimization》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试