C++<iomanip> control character

Source: Internet
Author: User

C++<iomanip> Control C + + cout output format

The following header files are often seen in C + + programs
#include <iomanip>
IO represents the input and output, MANIP is the abbreviation of the manipulator (manipulator)
The role of Iomanip:
Mainly for cin,cout, such as some manipulation operators, such as setfill,setw,setbase,setprecision and so on. It is the I/O flow control header file, just like the formatted output in C. Here are some common control functions:
Dec base is 10 equivalent to "%d"
Hex base is 16 equivalent to "%x"
The OCT has a base of 8 equivalent to "%o"
Setfill (' C ') set the fill character to C
Setprecision (n) Set display decimal precision is n bit
SETW (n) Set a field width of n characters
This control means that the output width is guaranteed to be N. Such as:
cout << SETW (3) << 1 << setw (3) << << SETW (3) << + << Endl; The output result is
1 10100 (the default is right-aligned) when the output length is greater than 3 o'clock (<<1000), SETW (3) does not work.
▲SETW (N) Usage: Popularly speaking is preset width
such as cout<<SETW (5)<<255<<endl;
The result is:
Spaces (space) 255
▲setfill (char c) Usage: that is, if there is already a unused width size in the preset width, fill with the character C that is set
such as Cout<<setfill (' @ ') &LT;&LT;SETW (5) <<255<<endl;
The result is:
@@255
▲setbase (int n): Converts a number to n binary.
such as Cout<<setbase (8) &LT;&LT;SETW (5) <<255<<endl;
Cout<<setbase (&LT;&LT;SETW) (5) <<255<<endl;
Cout<<setbase (<<255<<endl;)
The result is:
(space) (space) 377
Spaces (space) 255
Spaces (space) F F
▲setprecision usage
Use Setprecision (n) to control the number of floating-point numbers displayed by the output stream. The default stream output value for C + + is 6.
If Setprecision (n) is combined with setiosflags (ios::fixed), you can control the number of digits to the right of the decimal point. Setiosflags (ios::fixed) is a fixed-point representation of real numbers.
If combined with setiosnags (ios::scientific), you can control the number of decimal digits of exponential notation. Setiosflags (ios::scientific) is an exponential representation of real numbers.
Setiosflags (ios::fixed) fixed floating-point display
Setiosflags (ios::scientific) index indicates
Setiosflags (ios::left) Align Left
Setiosflags (ios::right) Align Right
Setiosflags (IOS::SKIPWS) ignores leading whitespace
Setiosflags (ios::uppercase) 16 Decimal Capital Output
Setiosflags (ios::lowercase) 16 binary lowercase output
Setiosflags (ios::showpoint) force display decimal point
Setiosflags (ios::showpos) force display symbols
Example:
#include <iostream.h>
#include <iomanip.h>
using namespace Std;
int main ()
{
cout<<12345.0<<endl;//Output "12345"
Cout<<setiosflags (ios::fixed) <<setprecision (3) <<1.2345<<endl; output "1.235"
Cout<<setiosflags (ios::scientific) <<12345.0<<endl;//output "1.234500e+004"
Cout<<setprecision (3) <<12345.0<<endl;//output "1.235e+004" (1.235e+004 should be changed to 1.23e+004)
return 0;
}

C++<iomanip> control character

C++<iomanip> control character

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.