Ostream (cout) Manipulators
A manipulator is a special value that is sent to an output stream
(such as cout) as if it were to be printed, but instead
of being printed, it changes the way that future things are
handled by that stream.
For example, there is the always familiar setw manipulator
that sets the minimum number of characters that the next value
printed must occupy.
cout <<12 << 34;
prints out the numbers 12 and 34 in the normal default way, which means
using the minumum number of digits and no sepration, so the outputs
run together producing 1234, which isn't usually what's wanted.
Of course, we could print spaces between values:
cout <<12 << " " << 34;
which solve the problem of values running together, but isn't a good solution
if we want to produce tabular output for example.
for (int i=1; i<500; i+=70)
cout << i << " cubed is " << i*i*i <<
" and squared is " << i*i << endl;
produces this output:
1 cubed is 1 and squared is 1
71 cubed is 357911 and squared is 5041
141 cubed is 2803221 and squared is 19881
211 cubed is 9393931 and squared is 44521
281 cubed is 22188041 and squared is 78961
351 cubed is 43243551 and squared is 123201
421 cubed is 74618461 and squared is 177241
491 cubed is 118370771 and squared is 241081
Which is less than perfect. The columns don't line up, especially the last one,
so it is harder than it should be to read off results.
Using setw, we can try to insist that each number is printed in 9 characters
(enough space for the biggest thing ever printed):
for (int i=1; i<500; i+=70)
cout << setw(9) << i << " cubed is " << i*i*i <<
" and squared is " << i*i << endl;
which produces this output:
1 cubed is 1 and squared is 1
71 cubed is 357911 and squared is 5041
141 cubed is 2803221 and squared is 19881
211 cubed is 9393931 and squared is 44521
281 cubed is 22188041 and squared is 78961
351 cubed is 43243551 and squared is 123201
421 cubed is 74618461 and squared is 177241
491 cubed is 118370771 and squared is 241081
That isn't exactly what was wanted. Unlike most other manipulators, setw
only modifies the single next thing printed. Others have a permanent effect until
explicitly switched off. So the correct version would be:
for (int i=1; i<500; i+=70)
cout << setw(3) << i << " cubed is " << setw(9) << i*i*i <<
" and squared is " << setw(6) << i*i << endl;
which finally produces the desired output:
1 cubed is 1 and squared is 1
71 cubed is 357911 and squared is 5041
141 cubed is 2803221 and squared is 19881
211 cubed is 9393931 and squared is 44521
281 cubed is 22188041 and squared is 78961
351 cubed is 43243551 and squared is 123201
421 cubed is 74618461 and squared is 177241
491 cubed is 118370771 and squared is 241081
The Manipulators
To make use of manipulators, you must #include both <iostream> and <iomanip>
in your program.
setw(n)
(applies only to next value printed)
Sets the minimum width. If the value would normally occupy more than n characters, nothing
happens. If it would normally occupy fewer than n characters, spaces are added to pad it out.
setprecision(n)
Controls the number of digits printed for floating point numbers. Normally this is not
the number of digits after the point, but the total number of digits. After setprecision(3),
the number 12345 is printed as 1.23e+04, the number 1.2345
is printed as 1.23, and the number 1.2 is printed as 1.20.
But if the fixed manipulator is turned on (see below),
it does control the number of digits after the point, rather than the total number of digits.
Turned 'off' by setprecision(0)
setbase(n)
Controls the base in whcih integer values are printed.
Warning The only accepted values for the base are 8, 10, and 16; any other value
is just ignored!
Turned 'off' by setbase(10)
setfill(c)
Sets the fill character. When setw forces padding to be added
to short values, this character is used instead of spaces.
Turned 'off' by setfill(' ')
setoisflags(ios::fixed)
Turns on the fixed manipulator, meaning that the setprecision manipulator
will now control the number of digits after the point, instead of the total number of
digits. Also forces all floats and doubles to be printed in non-scientific notation
(i.e. the "e+" and "e-" notation is never used. Normally the system
chooses scientific notation only when the number is too small or large to be printed
properly without it.
Turned off by resetoisflags(ios::fixed)
setoisflags(ios::left)
When setw causes fill characters to be added, they are normally inserted to the left
of (i.e. before) the value printed. Turning left on causes the fill characters
to be added to the right. The names seem reversed because they are talking about justification:
left, causes left justification.
Turned off by resetoisflags(ios::left)
setoisflags(ios::showbase)
When showbase is turned on,
if setbase has set the base
to 8, every integer printed (in base 8) has a leading zero printed before it;
if setbase has set the base
to 16, every integer printed (in base 16) has "0x" printed before it.
Turned off by resetoisflags(ios::showbase)
setoisflags(ios::showpoint)
Normally, floating point numbers are printed without trailing
zeros, and if they happen to be whole numbers, even the decimal
point is not printed. If showpoint is turned on, then
the decimal point and trailing zeros are always printed for
floats and doubles.
Turned off by resetoisflags(ios::showpoint)
setoisflags(ios::showpos)
Normally, negative numbers are printed with a "-"
in front of them, and positive numbers are left undecorated.
Under the influence of showpos, positive numbers
also get a "+" printed before them.
Turned off by resetoisflags(ios::showpos)
setoisflags(ios::uppercase)
When setbase has set the base to 16, the digits
above 9 are represented by the letters abcdef.
Ander the influence of uppercase, the digits are
ABCDEF instead.
Uppercase does not cause strings to be converted to capitals.
Turned off by resetoisflags(ios::uppercase)
setoisflags(ios::scientific)
This is simply an alternative way of turning off fixed,
it does not force all numbers to be printed in scientific notation.