Format Control for C + + output streams

Source: Internet
Author: User

Inline Smanip (long) resetiosflags (long _l) {return Smanip (long) (__resetiosflags, _l);}
inline Smanip (int) setfill (int _m) {return smanip (int) (__setfill, _m);}
Inline Smanip (long) setiosflags (long _l) {return Smanip (long) (__setiosflags, _l);}
inline Smanip (int) setprecision (int _p) {return smanip (int) (__setprecision, _p);}
inline Smanip (int) setw (int _w) {return smanip (int) (__SETW, _w);}

They're all inline inline functions.

(1) setw (int n) Preset output width

Example: COUT<<SETW (6) <<123<<endl;

The output is "123", and in front of 123 there will be 3 spaces, 123 right-aligned.

(2) Setfill (char c) preset padding characters

such as: Cout<<setfill (' # ') <<123<<endl;

The output shows the result "# # #123", 123 right-aligned, and 3 ' # ' in front.

(3) setbase (int n) preset integer output binary

Example: Cout<<setbase (8) <<255<<endl;

The output shows a result of 377

(4) setprecision (int n) is used to control the accuracy of the output stream display floating point numbers, and the integer n represents the number of floating-point number displayed. The sample program is as follows:

#include <iostream.h>

#include <iomanip.h>//format control

void Main ()

{

Double amount = 22.0/7;

cout <<amount <<endl; (1)

cout <<setprecision (0) <<amount <<endl//(2)

<<setprecision (1) <<amount <<endl//(3)

<<setprecision (2) <<amount <<endl//(4)

<<setprecision (3) <<amount <<endl//(5)

<<setprecision (4) <<amount <<endl; (6)

cout <<setiosflags (ios::fixed);

cout <<setprecision (8) <<amount <<endl; (7)

cout <<setiosflags (ios::scientific) <<amount<<endl; (8)

cout <<setprecision (6);

}

The number of valid digits is not set before the 1th line output value, so the number of significant digits of the stream is set by default for the value 6: The 2nd output sets the number of significant digits 0,c++ the minimum number of significant digits is 1, so as the number of significant digits is set to one view: the 3rd to 6th line output by the set number of significant digits output. The 7th line of output is shared with Setiosflags (ios::fixed). So Setprecision (8) sets the number of digits after the decimal point, not the number of digits. The 8th line of output uses Setiosflags (ios::scientific) to represent the output form of an exponential representation. Its significant number of digits is inherited from the last set value of 8. When outputting in exponential form, Setprecision (n) represents the number of decimal digits. (But the output of this exponential form I did not run out with VC6).

SETW (int _w) explanation
<< setw (int _w) << output bit width
such as << SETW (7) << 1000 output as "1000" (immediately following three spaces)
And such as << SETW (3) << 1000 bit width is less than the original digit width then output "1000" in accordance with the original format and no space


Setfill () explanation
Setfill (int _m) is used with SETW (int _w) to fill the specified character with a space after set (int _w)
Note that the argument inside the Setfill is int _m is an integer so if we want to fill the * number with a space
To use Setfill (' * ') [character] instead of Setfill (*)

TELLP () function and TELLG () function in C + +
The TELLP () function is used to get the current position of the "Output pointer" (the number of bytes from the beginning of the file to the current position);
The TELLG () function is used to get the current position of the read-in pointer (the number of bytes from the beginning of the file to the current position).

Turn!!! http://ghyan1208.blog.163.com/blog/static/1628666762011589311449/

Format Control for C + + output streams

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.