T.setf(ios::scientific, ios::floatfield);
T << endl << f << endl;
2.30011450000000000000e+06
T.setf(ios::fixed, ios::floatfield);
T << f << endl;
2300114.50000000000000000000
T.width(10);
Is there any more?
T.width(40);
0000000000000000000000Is there any more?
T.setf(ios::left, ios::adjustfield);
T.width(40);
Is there any more?0000000000000000000000
Studying this output should clarify your understanding of the iostream formatting member functions.
Manipulators
As you can see from the previous program, calling the member functions for stream formatting operations can get a bit tedious. To make things easier to read and write, a set of
Manipulators change the state of the stream instead of (or in addition to) processing data. When you insert endl in an output expression, for example, it not only inserts a newline character, but it also
cout << flush;
which causes a call to the flush( ) member function, as in
cout.flush();
as a side effect (nothing is inserted into the stream). Additional basic manipulators will change the number base to oct (octal), dec (decimal) or hex (hexadecimal):.
cout << hex << "0x" << i << endl;
In this case, numeric output will continue in hexadecimal mode until you change it by inserting either dec or oct in the output stream.
There’s also a manipulator for extraction that "eats" white space:.
cin >> ws;
Manipulators with no arguments are provided in
Manipulator | Effect |
---|---|
showbase noshowbase | Indicate the numeric base (dec, oct, or hex) when printing an integral value. The format used can be read by the C++ compiler. |
showpos noshowpos | Show plus sign (+) for positive values. |
uppercase nouppercase | Display uppercase A-F for hexadecimal values, and display E for scientific values. |
showpoint noshowpoint | Show decimal point and trailing zeros for floating-point values. |
skipws noskipws | Skip white space on input. |
left right internal | Left-align, pad on right. Right-align, pad on left. Fill between leading sign or base indicator and value. |
scientific fixed | Indicates the display preference for floating-point output (scientific notation vs. fixed-point decimal). |
Manipulators with arguments
There are six standard manipulators, such as setw( ), that take arguments. These are defined in the header file <iomanip>, and are summarized in the following table.