The <iomanip>
header declares several I/O manipulators. An I/O manipulator is a function 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 and clearer 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 10 for a thorough discussion of I/O, including manipulators.
The return type of each manipulator is implementation-defined. For the following function descriptions, this type is shown as manip_t.
A manipulator is used by applying to a stream, that is, out <<
manip where out is an instance of basic_ostream
, or in >>
manip where 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.
Clears specified flags
manip_t resetiosflags(ios_base::fmtflags mask)
The resetiosflags
function clears the flag bits in mask for stream. In other words, it performs the equivalent of stream.setf(ios_base::fmtflags(0),
mask)
.
setiosflags function, ios_base in <ios>
Sets conversion radix
manip_t setbase(int base)
The setbase
function sets the conversion radix for stream. In other words, it performs the equivalent of stream.setf(
newbase,
ios_base::basefield)
, where newbase depends on base
, as shown in Table 13-11.
setbase argument |
fmtflags equivalent |
---|---|
8 | ios_base::oct |
10 | ios_base::dec |
16 | ios_base::hex |
Anything else | 0 |
ios_base in <ios>
Sets pad character
template <typename charT> manip_t setfill(charT c)
The setfill
function template sets the fill character for stream to c
. In other words, it performs the equivalent of stream.fill(c)
.
ios_base in <ios>
Sets specified flags
manip_t setiosflags(ios_base::fmtflags mask)
The setiosflags
function sets the flag bits in mask for stream. In other words, it performs the equivalent of stream.setf(mask)
.
resetiosflags function, ios_base in <ios>
Sets precision
manip_t setprecision(int n)
The setprecision
function template sets the output precision for stream to n
. In other words, it performs the equivalent of stream.precision(n)
.
ios_base in <ios>
Sets field width
manip_t setw(int n)
The setw
function template sets the output field width for stream to n
. In other words, it performs the equivalent of stream.width(n)
.
ios_base in <ios>