Contents|Index|Previous|Next
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>.
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 as an 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).
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>.
Top|Contents|Index|Previous|Next