C++ formatted output

Each output stream has a precision member function. Once the precision function is called for a certain output stream, when outputting a number with a decimal point to the stream, either a total of two significant digits are reserved, or two digits after the decimal point are reserved. The specific implementation method Determined by the compiler. The precision function is only effective for the specified stream.
The setf member function is short for set flags, not for set format! It is an instruction to do one thing in the alternative way.

The ios::fixed flag causes the stream to use fixed-point notation to output double-type numbers instead of using e notation

The ios::showpoint flag requires the stream to always include a decimal point in floating point numbers.

The ios::scientific flag will output numbers in e-notation.

ios::showpos will output a + sign before a positive integer

If the ios::right flag is used, and width is used to specify the field width, the next item of output will be right-aligned. The default is this

If the ios::left flag is used, and width is used to specify the field width, the next output item will be left-aligned with the width member function to set the field width. E.g:

cout << "a";    
    cout.width(4);
    cout << 7 << endl;

Then output a 7. There are 3 spaces between a and 7, because the system defaults to set ios::right to align right.

Note that the width member function is only suitable for the next item to be output. If you want to output multiple items, you need to call width multiple times. If you find it troublesome, you can use the stream manipulation element setw.

Any flag that is set can be cancelled by calling the unsetf member function, for example:

  cout.unsetf(ios::showpos);

You can cancel the + sign before the positive integer.

setw and setprecision manipulators.

操纵元是以非传统的方式调用的函数。操纵元位于插入操作符 << 之后。两个操纵元均在 iomanip 库中,要想使用,需要调用该库。

The setw manipulator has the same function as the width member function. If you want to set the field width multiple times, you need to call the setw manipulator multiple times. In fact, the setw manipulator calls the width member function. The following code:

  cout << "a" << set(5) << 1    
                << set(5) << 2
                << set(7) << 3 << endl;
                

The output of this sentence is: a 1 2 3. There are 4 spaces before 1, 2 and 6 spaces before 3.

  setprecision 操纵元和 precision 成员函数功能一样。如下代码:
  cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout << "a" << setprecision(2) << 1.2
                                 << 2.3 << endl;

The setprecision manipulator is the same as the precision member function, and it is always effective after setting. The output of the above example: 1.20 2.30.
It is better to apply what you have learned, and there may be shortcomings in the above, welcome to point out the discussion.
(More free C/C++, Linux, Nginx, ZeroMQ, MySQL, Redis, fastdfs, MongoDB, ZK, streaming media, CDN, P2P, K8S, Docker, TCP/IP, coroutine, DPDK, etc. multiple knowledge points dry goods Learning materials plus group 960994558)

Guess you like

Origin blog.csdn.net/weixin_52622200/article/details/110404678