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

xiaoxiao2021-02-28  17

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::setiosflagsint 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). 2345678910// 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; } Edit & Run 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 10hex, if base is 16oct, if base is 8zero, if base is any other value. 23456789// 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; } Edit & Run 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). 23456789// 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; } Edit & Run 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; } Edit & Run 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; } Edit & Run Output: 77
转载请注明原文地址: https://www.6miu.com/read-2449988.html

最新回复(0)