C ++ <iomanip> controller and iomanip Controller
C ++ <iomanip> controller c ++ cout output format
The following header files are often seen in c ++ programs.
# Include <iomanip>
Io stands for input and output, and manip stands for manipulator.
The role of iomanip:
This mainly applies to some operations such as cin and cout, such as setfill, setw, setbase, and setprecision. It is an I/O traffic control header file, just like formatted output in C. The following are some common control functions:
The base number of dec is 10, which is equivalent to "% d"
The base number of hex is 16, which is equivalent to "% X"
The base number of oct is 8, which is equivalent to "% o"
Setfill ('C') is set to c
Setprecision (n) is set to show n decimal places
Setw (n): Set the domain width to n characters.
This control operator ensures that the output width is n. For example:
Cout <setw (3) <1 <setw (3) <10 <setw (3) <100 <endl; the output result is
1 10100 (right-aligned by default) setw (3) does not work when the output length is greater than 3 (<1000.
▲Setw (n) usage: in general, it is the preset width.
For example, cout <Setw (5)<255 <endl;
The result is:
(Space) 255
▲Setfill (char c) usage: Fill in the specified character c if the preset width already has a useless width.
For example, cout <setfill ('@') <setw (5) <255 <endl;
The result is:
@ 255
▲Setbase (int n): converts a number to n-base.
For example, cout <setbase (8) <setw (5) <255 <endl;
Cout <setbase (10) <setw (5) <255 <endl;
Cout <setbase (16) <255 <endl;
The result is:
(Space) 377
(Space) 255
(Space) f
▲Setprecision usage
You can use setprecision (n) to control the number of floating point numbers displayed in the output stream. The default value of C ++ is 6.
If setprecision (n) is used with setiosflags (iOS: fixed), you can control the number of digits on the right of the decimal point. Setiosflags (ios: fixed) is a real number expressed by a fixed point.
If used with setiosnags (ios: scientific), you can control the number of decimal places in exponential notation. Setiosflags (ios: scientific) is an exponential representation of real numbers.
Setiosflags (ios: fixed) fixed floating point display
Setiosflags (ios: scientific) index Representation
Setiosflags (ios: left) left alignment
Setiosflags (ios: right) right-aligned
Setiosflags (ios: skipws) Ignore leading Blank
Setiosflags (ios: uppercase) hexadecimal number uppercase output
Setiosflags (ios: lowercase) hexadecimal lowercase output
Setiosflags (ios: showpoint) Forcibly displays the decimal point
Setiosflags (ios: showpos) force display symbol
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> Controller