cout格式化输出

左手的ㄟ右手 2022-08-08 00:47 349阅读 0赞

转自:http://www.cnblogs.com/walfud/articles/2047096.html

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

  1. ostream::fmtflags old
  2. =
  3. cout.flag() ;
  4. //
  5. 无参将返回当前 flag
  6. cout.flag(old) ;
  7. //
  8. 恢复到原先保存的值

将 bool 值以 literals 输出

  1. cout
  2. <<
  3. "
  4. numeric :
  5. "
  6. <<
  7. true
  8. <<
  9. "
  10. or
  11. "
  12. <<
  13. false
  14. <<
  15. endl ;
  16. //
  17. 1 or 0
  18. cout
  19. <<
  20. "
  21. literals :
  22. "
  23. <<
  24. boolalpha
  25. <<
  26. true
  27. <<
  28. "
  29. or
  30. "
  31. <<
  32. false
  33. <<
  34. endl ;
  35. //
  36. true or false
  37. cout
  38. <<
  39. "
  40. literals :
  41. "
  42. <<
  43. boolalpha
  44. <<
  45. 0
  46. <<
  47. endl ;
  48. //
  49. 0 原因: 0 cout中不等价于 false

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

将 bool 值以 numeric 输出

  1. cout
  2. <<
  3. "
  4. numeric :
  5. "
  6. <<
  7. noboolalpha
  8. <<
  9. true
  10. <<
  11. "
  12. or
  13. "
  14. <<
  15. false
  16. <<
  17. endl ;
  18. //
  19. 1 or 0

从此以后, cout 对 bool 值的输出将恢复 numeric 格式

指定 Integral Values 的 Base

  1. const
  2. int
  3. ival
  4. =
  5. 17
  6. ;
  7. //
  8. 'ival' is constant, so value never change
  9. cout
  10. <<
  11. "
  12. oct :
  13. "
  14. <<
  15. oct
  16. <<
  17. ival
  18. <<
  19. endl ;
  20. //
  21. 21 : 8 进制
  22. cout
  23. <<
  24. "
  25. dec :
  26. "
  27. <<
  28. dec
  29. <<
  30. ival
  31. <<
  32. endl ;
  33. //
  34. 17 : 10 进制
  35. cout
  36. <<
  37. "
  38. hex :
  39. "
  40. <<
  41. hex
  42. <<
  43. ival
  44. <<
  45. endl ;
  46. //
  47. 11 : 16 进制
  48. cout
  49. <<
  50. "
  51. hex :
  52. "
  53. <<
  54. hex
  55. <<
  56. 17.01
  57. <<
  58. endl ;
  59. //
  60. 17.01 : 不受影响

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

显示表明 Integer Values 的 Base

复制代码

  1. cout
  2. <<
  3. showbase ;
  4. //
  5. Show base when printing integral values
  6. cout
  7. <<
  8. "
  9. oct :
  10. "
  11. <<
  12. oct
  13. <<
  14. ival
  15. <<
  16. endl ;
  17. //
  18. 21 : 8 进制
  19. cout
  20. <<
  21. "
  22. dec :
  23. "
  24. <<
  25. dec
  26. <<
  27. ival
  28. <<
  29. endl ;
  30. //
  31. 017 : 10 进制
  32. cout
  33. <<
  34. "
  35. hex :
  36. "
  37. <<
  38. hex
  39. <<
  40. ival
  41. <<
  42. endl ;
  43. //
  44. 0x11 : 16 进制
  45. cout
  46. <<
  47. "
  48. hex :
  49. "
  50. <<
  51. hex
  52. <<
  53. 17.01
  54. <<
  55. endl ;
  56. //
  57. 17.01 : 不受影响
  58. cout
  59. <<
  60. noshowbase ;
  61. //
  62. Reset state of the stream

复制代码

若想改变16进制字母的大小, 可以结合 uppercase/nouppercase

  1. cout
  2. <<
  3. showbase
  4. <<
  5. uppercase ;
  6. cout
  7. <<
  8. "
  9. hex :
  10. "
  11. <<
  12. hex
  13. <<
  14. 15
  15. <<
  16. endl ;
  17. //
  18. 0XF 大写形式
  19. cout
  20. <<
  21. nouppercase ;
  22. cout
  23. <<
  24. "
  25. hex :
  26. "
  27. <<
  28. hex
  29. <<
  30. 15
  31. <<
  32. endl ;
  33. //
  34. 0xf 小写形式

showbase 与 noshowbase 的作用周期也是 persistent

对于 float/double 型, 有三种格式化控制

一: 输出精度 precision : by default is 6 pricision
控制了至多一共会输出多少个数字.
当要输出的数字多余指定的值时, 将发生 四舍五入(rounded);
当要输出的数字少于指定的值时, 则实际输出的数字个数将少于指定值.

  1. //
  2. cout.pricision(4) ;
  3. //
  4. 等价于 cout <<setprecision(4) ;
  5. cout
  6. <<
  7. setprecision(
  8. 4
  9. )
  10. <<
  11. 12.345678
  12. <<
  13. endl ;
  14. //
  15. 12.35 rounded!
  16. cout
  17. <<
  18. setprecision(
  19. 10
  20. )
  21. <<
  22. 12.345678
  23. <<
  24. endl ;
  25. //
  26. 12.345678 其实内部发生了 rounded, 而结果正好进位, 与原值相同
  27. cout
  28. <<
  29. cout.precision()
  30. <<
  31. endl ;
  32. //
  33. 输出当前精度

二: 表现形式 notation : ‘very large and very small values are printed using scientific notation. other values use fixed decimal.’
notation 控制了输出的形式 : 科学计数法(scientific) 和 定点小数(fixed)

  1. float
  2. f
  3. =
  4. 101
  5. /
  6. 6.0
  7. ;
  8. cout
  9. <<
  10. fixed
  11. <<
  12. f
  13. <<
  14. endl ;
  15. //
  16. 16.83334 : 小数点后共6
  17. cout
  18. <<
  19. scientific
  20. <<
  21. f
  22. <<
  23. endl ;
  24. //
  25. 1.683333e+001 : 小数点后共6

恢复到初始状态

  1. cout.unsetf(ostream::floatfield) ;
  2. //
  3. Retrieve to default handling for notation
  4. cout
  5. <<
  6. f
  7. <<
  8. endl ;
  9. //
  10. 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.’

  1. cout
  2. <<
  3. 10.0
  4. <<
  5. endl ;
  6. //
  7. 10
  8. cout
  9. <<
  10. showpoint
  11. <<
  12. 10.0
  13. <<
  14. endl ;
  15. //
  16. 10.0000
  17. cout
  18. <<
  19. noshowpoint
  20. <<
  21. endl ;
  22. //
  23. Revert to default handling of decimal

输出填充 Padding the Output

*setw to specify the minimum space for the next numeric or string value.*

  1. cout
  2. <<
  3. setw(
  4. 10
  5. )
  6. <<
  7. 12.3
  8. <<
  9. endl ;
  10. //
  11. ______12.3
  12. cout
  13. <<
  14. setw(
  15. 10
  16. )
  17. <<
  18. 12
  19. <<
  20. 3
  21. <<
  22. endl ;
  23. //
  24. ________123
  25. cout
  26. <<
  27. setw(
  28. 3
  29. )
  30. <<
  31. 12.345
  32. <<
  33. endl ;
  34. //
  35. If the total output is more than 3, it can be extended

2011051520540965.png

*left to left-justify the output.*

  1. cout
  2. <<
  3. left ;
  4. //
  5. left-justify
  6. cout
  7. <<
  8. setw(
  9. 5
  10. )
  11. <<
  12. 12
  13. <<
  14. setw(
  15. 5
  16. )
  17. <<
  18. 34
  19. <<
  20. endl ;
  21. //
  22. 12___34___

2011051520570056.png

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

  1. cout
  2. <<
  3. right ;
  4. //
  5. By default
  6. cout
  7. <<
  8. setw(
  9. 5
  10. )
  11. <<
  12. 12
  13. <<
  14. setw(
  15. 5
  16. )
  17. <<
  18. 34
  19. <<
  20. endl ;
  21. //
  22. 12___34___

2011051520582023.png

*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)*

  1. cout
  2. <<
  3. internal
  4. ;
  5. //
  6. By default
  7. cout
  8. <<
  9. setw(
  10. 5
  11. )
  12. <<-
  13. 12
  14. <<
  15. endl ;
  16. //
  17. 12___34___

2011051521040668.png

*setfill lets us specify an alternative character to use when padding the output. By default, the value is a space.*

  1. cout
  2. <<
  3. setfill(
  4. '
  5. *
  6. '
  7. ) ;
  8. //
  9. By default
  10. cout
  11. <<
  12. setw(
  13. 5
  14. )
  15. <<
  16. 12
  17. <<
  18. endl ;
  19. //
  20. 12___34___

2011051521054047.png

Header Files

Manipulators Defined in < iomanip>

  1. setfill(
  2. char
  3. ch) Fill whitespace with
  4. '
  5. ch
  6. '
  7. setprecision(
  8. int
  9. n) Set floating
  10. -
  11. point precision to
  12. '
  13. n
  14. '
  15. setw(
  16. int
  17. w) Read or write value to
  18. '
  19. w
  20. '
  21. characters
  22. setbase(
  23. int
  24. b) Output integers
  25. in
  26. base
  27. '
  28. b
  29. '
  30. (only
  31. '
  32. b
  33. '
  34. is
  35. 8
  36. /
  37. 10
  38. /
  39. 16
  40. could the function work)

发表评论

表情:
评论列表 (有 0 条评论,349人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C++ cout格式化输出

    希望按照一定的格式进行输出,如按十六进制输出整数,输出浮点数时保留小数点后面两位,输出整数时按 6 个数字的宽度输出,宽度不足时左边补 0,等等。C++ 中的 cout 对象则

    相关 cout格式化输出

    在C语言中,我们一般用printf()函数来进行输出,通过输出字符串中的格式说明符(如%4.2d)可以很容易地格式化输出。而在C++中,为简便起见,往往不指定输出的格式,由系统

    相关 cout格式化输出

    转载于:[cout格式化输出][cout] 在C语言中,我们一般用printf()函数来进行输出,通过输出字符串中的格式说明符(如%4.2d)可以很容易地格式化输出。而在C+