Manipulators

  1. 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.

  2. A manipulator function in turn calls a member function.

  3. Manipulator functions may or may not have arguments. We are already familiar with one manipulator, endl, which has no arguments.

  4. 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.

  5. Except with the way how the function is called, the manipulator setprecision does exactly the same thing as the member function precision.

  6. As you can see, manipulators are easier to write than their corresponding member functions.

  7. In order to use the manipulators setw and setprecision you must include the following directive in your program

    #include <iomanip>