f78
Changing stream properties using manipulators
For convenience, manipulators provide a way to change certain properties of streams, or otherwise affect them, in the middle of expressions involving << or >>.
For example, you might use the following input statement to produce |**234| as output.
cout << "|" << setfill('*') << setw(5) << 234 << "|";
Manipulators that take an argument require #include <iomanip.h>.
ws Manipulator
Skips whitespace.
flush Manipulator
Flushes an output stream.
For instance, the input, cout<<...<<flush;,
has the same effect as the input,
cout<<...; cout.flush();.
endl Manipulator
Writes an end of line character, \n, then flushes the output stream.
ends Manipulator
Writes the string terminator character, \0.
setprecision (int signif) Manipulator
You can change the value
of ios::precision
in <<
expressions with the manipulator, setprecision(signif);
for instance, use the following input to print 4.6.
Manipulators such as setprecision(signif)
that take an argument
require
#include <iomanip.h>.
cout << setprecision(2) << 4.567;
setw (int n) Manipulator
You can change the value of ios::width in << expressions with the manipulator setw( n); use the following input statement example.
cout << setw(5) << 234;
This input prints 234
with two leading spaces.
Requires #include
<iomanip.h>.
setbase (int base) Manipulator
Where base
is one of 10
(decimal), 8
(octal), or 16
(hexadecimal), change the base value for numeric representations.
Requires #include
<iomanip.h>.
dec Manipulator
Select decimal base; equivalent to setbase(10).
hex Manipulator
Select hexadecimal base; equivalent to setbase(16)< 4c2 /FONT>.
oct Manipulator
Select octal base; equivalent to setbase(8).
setfill (char padding) Manipulator
Set the padding
character, in the same way as ios::fill.
Requires #include
<iomanip.h>.