Manipulators
-
Manipulator functions are called in a nontraditional way.
Manipulators are placed after the insertion operator <<,
just as if the manipulator function call were an item to be output.
-
A manipulator function in turn calls a member function.
-
Manipulator functions may or may not have arguments. We are already familiar with one manipulator, endl, which has no arguments.
-
The manipulator setw calls the member function width and sets the field width of the next item that is output. For example the code
cout << "Start" << setw(4) << 10
<< setw(4) << 20
<< setw(6) << 30;
will produce the following output
Start 10 20 30
(There are 2 spaces before the 10, 2 spaces before the 20, and 4 spaces before the 30.
-
Except with the way how the function is called, the manipulator setprecision does exactly the same thing as the member function precision.
-
As you can see, manipulators are easier to write than their corresponding member functions.
-
In order to use the manipulators setw and setprecision you must include the following directive in your program
#include <iomanip>