c++格式化输出-------#include<iomanip>

io代表输入输出,manip是manipulator(操纵器)的缩写-------来自于百度百科

具体函数:

1.setiosflags  /*unspecified*/ setiosflags (ios_base::fmtflags mask);

    setiosflags的参数是该流的格式标志值,这个值由如下位掩码(ios枚举器)指定,并可用位或OR(|)运算符进行组合:

    ios::skipws  在输人中跳过空白。    
    ios::left  左对齐值,用填充字符填充右边。
    ios::right  右对齐值;用填充字符填充左边(缺省对齐方式)。
    ios::internal  在指定任何引导标记或基之后增加填充字符。
    ios::dec  以基10(十进制)格式化数值(缺省进制)。
    ios::oct  以基8(八进制)格式化数值。
    ios::hex  以基16(十六进制)格式化数值。
    ios::showbase  以C++编译器能读的格式显示数值常量。
    ios::showpoint  对浮点数值显示小数点和尾部的0。
    ios::uppercase  对于十六进制数值显示大写字母A到F,对于科学格式显示大写字母E。
    ios::showpos  对于正数显示正号(+)。
    ios::scientific  以科学格式显示浮点数值。
    ios::fixed  以定点格式显示浮点数值。
    ios::unitbuf  导致在每次插入之后ostream::osfx刷新该流。缺省地,cerr是缓冲的单元。
    ios::stdio  导致在每次插入之后ostream::osfx刷新该流的stdout和stderr。 

// setiosflags example
#include <iostream>     // std::cout, std::hex, std::endl
#include <iomanip>      // std::setiosflags

int main () {
  std::cout << std::hex;
  std::cout << std::setiosflags (std::ios::showbase | std::ios::uppercase);
  std::cout << 100 << std::endl;
 
return 0;
}

Output:


0X64

2.resetiosflags:/*unspecified*/ resetiosflags (ios_base::fmtflags mask);

Reset format flags
Unsets the format flags specified by parameter mask.

Behaves as if member unsetf were called with mask as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams).

2
3
4
5
6
7
8
9
10
// resetiosflags example
#include <iostream>     // std::cout, std::hex, std::endl
#include <iomanip>      // std::setiosflags, std::resetiosflags

int main () {
  std::cout << std::hex << std::setiosflags (std::ios::showbase);
  std::cout << 100 << std::endl;
  std::cout << std::resetiosflags(std::ios::showbase) << 100 << std::endl;
  return 0;
}


This code first sets the showbase flag and then resets it using the resetiosflags manipulator. Output:

0x64
64

3.setbase:/*unspecified*/ setbase (int base);

Sets the basefield to one of its possible values: dec, hex or oct, according to argument base.

Behaves as if setf(which,ios_base::basefield) were called on the stream on which it is inserted/extracted as a manipulator, with which being:

  • dec, if base is 10
  • hex, if base is 16
  • oct, if base is 8
  • zero, if base is any other value.

2
3
4
5
6
7
8
9
// setbase example #include <iostream>     // std::cout, std::endl #include <iomanip>      // std::setbase int main ()     std::cout << std::setbase(16);     std::cout << 110 << std::endl;     return 0; }


This code uses the setbase manipulator to set hexadecimal as the basefield selective flag.

Output:
6e
4.setfill:
/*unspecified*/ setfill (char_type c);
Set fill character
Sets c as the stream's fill character.

Behaves as if member fill were called with c as argument on the stream on which it is inserted as a manipulator (it can be inserted on output streams).

2
3
4
5
6
7
8
9
// setfill example #include <iostream>// std::cout, std::endl #include <iomanip> // std::setfill, std::setw int main () {
  std::cout << std::setfill ('x') << std::setw (10); std::cout << 77 << std::endl; return 0;
}


Output:

xxxxxxxx77







5.setprecision:设置有效数字用

/*unspecified*/ setprecision (int n);
Set decimal precision

Sets the decimal precision to be used to format floating-point values on output operations.

Behaves as if member precision were called with n as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams).

/ setprecision example #include <iostream>     // std::cout, std::fixed #include <iomanip>      // std::setprecision int main () {   double f =3.14159;      std::cout << std::setprecision(5) << f << '\n';     std::cout << std::setprecision(9) << f << '\n'    std::cout << std::fixed;
  std::cout << std::setprecision(5) << f << '\n'    std::cout << std::setprecision(9) << f << '\n'    return 0; }


Output:
3.1416
3.14159
3.14159
3.141590000

6.setw设置域宽

/*undefined*/ setw (int n);
Set field width
Sets the field width to be used on output operations.

Behaves as if member width were called with n as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams).
// setw example
#include <iostream>     // std::cout, std::endl
#include <iomanip>      // std::setw

int main () {
  std::cout << std::setw(10);
  std::cout << 77 << std::endl;
  return 0;
}


Output:
        77

猜你喜欢

转载自blog.csdn.net/newandbetter/article/details/80336853