Oracle® Solaris Studio 12.4: C++ User's Guide

Exit Print View

Updated: March 2015
 
 

13.7 Manipulators

Manipulators are values that you can insert into or extract from iostreams to have special effects.

Parameterized manipulators are manipulators that take one or more parameters.

Because manipulators are ordinary identifiers and therefore use up possible names, iostream does not define them for every possible function. A number of manipulators are discussed with member functions in other parts of this chapter.

The 13 predefined manipulators are described in the following table. This table assumes the following:

  • i has type long.

  • n has type int.

  • c has type char.

  • istr is an input stream.

  • ostr is an output stream.

Table 13-2  iostream Predefined Manipulators
Predefined Manipulator
Description
1
ostr << dec, istr >> dec
Makes the integer conversion base 10.
2
ostr << endl
Inserts a newline character (’\n’) and invokes ostream::flush().
3
ostr << ends
Inserts a null (0) character. Useful when dealing with strstream.
4
ostr << flush
Invokes ostream::flush().
5
ostr << hex, istr >> hex
Makes the integer conversion base 16.
6
ostr << oct, istr >> oct
Make the integer conversion base 8.
7
istr >> ws
Extracts whitespace characters (skips whitespace) until a non-whitespace character is found (which is left in istr).
8
ostr << setbase(n), istr >> setbase(n)
Sets the conversion base to n (0, 8, 10, 16 only).
9
ostr << setw(n), istr >> setw(n)
Invokes ios::width(n). Sets the field width to n.
10
ostr << resetiosflags(i), istr>> resetiosflags(i)
Clears the flags bitvector according to the bits set in i.
11
ostr << setiosflags(i), istr >> setiosflags(i)
Sets the flags bitvector according to the bits set in i.
12
ostr << setfill(c), istr >> setfill(c)
Sets the fill character (for padding a field) to c.
13
ostr << setprecision(n), istr >> setprecision(n)
Sets the floating-point precision to n digits.

To use predefined manipulators, you must include the file iomanip.h in your program.

You can define your own manipulators. The two basic types of manipulators are:

  • Plain manipulator – Takes an istream&, ostream&, or ios& argument, operates on the stream, and then returns its argument.

  • Parameterized manipulator – Takes an istream&, ostream&, or ios& argument, one additional argument (the parameter), operates on the stream, and then returns its stream argument.