13.26 <iomanip>

The <iomanip> header declares several I/O manipulators. An I/O manipulator is a function object that can be used in a sequence of input or output operators to manipulate the I/O stream. The manipulators are simple wrappers for functionality that is available as member functions of the ios_base class, but manipulators are simpler to use in some situations.

For example, to print formatted output, the following two code fragments are equivalent:

// Using manipulators

std::cout << std::setw(16) << std::setprecision(12) << x;

   

// Without manipulators

std::cout.width(16);

std::cout.precision(12);

std::cout << x;

At a basic level, manipulators are easy to use. If you want to understand exactly how they work, perhaps to write your own, see Chapter 9 for a thorough discussion of I/O, including manipulators.

figs/acorn.gif

The return type of each manipulator is implementation-defined. For the following function descriptions, this type is shown as manip_t.

Use a manipulator by applying it to a streamthat is, out << manip, in which out is an instance of basic_ostream, or in >> manip, in which in is an instance of basic_istream. In the following function descriptions, stream refers to the input or output stream to which the manipulator is being applied.

resetiosflags function Clears specified flags

manip_t resetiosflags(ios_base::fmtflags mask)

The resetiosflags function clears the flag bits in mask for a stream. In other words, it performs the equivalent of stream.setf(ios_base::fmtflags(0), mask).

See Also

setiosflags function, ios_base in <ios>

setbase function Sets conversion radix

manip_t setbase(int base)

The setbase function sets the conversion radix for a stream. In other words, it performs the equivalent of stream.setf(newbase, ios_base::basefield), in which newbase depends on base, as shown in Table 13-11. Notice that any value of a base other than 8, 10, or 16 is treated the same as 0.

Table 13-11. Conversion radix

setbase argument

fmtflags equivalent

8

ios_base::oct

10

ios_base::dec

16

ios_base::hex

Anything else

0

See Also

ios_base in <ios>

setfill function template Sets pad character

template <typename charT>

manip_t setfill(charT c)

The setfill function template sets the fill character for a stream to c. In other words, it performs the equivalent of stream.fill(c).

See Also

ios_base in <ios>

setiosflags function Sets specified flags

manip_t setiosflags(ios_base::fmtflags mask)

The setiosflags function sets the flag bits in mask for a stream. In other words, it performs the equivalent of stream.setf(mask).

See Also

resetiosflags function, ios_base in <ios>

setprecision function Sets precision

manip_t setprecision(int n)

The setprecision function template sets the output precision for a stream to n. In other words, it performs the equivalent of stream.precision(n).

See Also

ios_base in <ios>

setw function Sets field width

manip_t setw(int n)

The setw function template sets the output field width for a stream to n. In other words, it performs the equivalent of stream.width(n).

See Also

ios_base in <ios>