C++ Reference ยท Strings and I/O
C++ <iomanip> header
Reference for the C++ <iomanip> manipulators: setw, setfill, setprecision, setbase, setiosflags, quoted and get_money, with sticky versus one-shot rules.
<iomanip> declares the manipulators that take an argument: std::setw, std::setfill, std::setprecision, std::setbase, std::setiosflags, std::resetiosflags, std::quoted and the money and time helpers. A manipulator is inserted into a stream with << (or extracted with >>) like a value, and it changes how later items are formatted.
The argument-free manipulators (std::hex, std::fixed, std::left, std::boolalpha, ...) live in <ios> and <iostream>. Together they are the stream way of doing what printf flags and format specifications do.
#include <iomanip> #include <iostream> std::cout << std::setw(6) << std::setfill('0') << 42; // 000042
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
/* unspecified */ setw(int n); /* unspecified */ setfill(char c); /* unspecified */ setprecision(int n); /* unspecified */ setbase(int base); // 8, 10 or 16 /* unspecified */ setiosflags(std::ios_base::fmtflags mask); /* unspecified */ resetiosflags(std::ios_base::fmtflags mask); /* unspecified */ quoted(const std::string& s, char delim = '"', char escape = '\\'); /* unspecified */ get_money / put_money / get_time / put_time
| Name | Description |
|---|---|
n (setw) |
Minimum width of the next formatted item. |
c (setfill) |
Padding character used when the item is shorter than the width. Default space. |
n (setprecision) |
Digits after the point with fixed / scientific; significant digits otherwise. |
base |
8, 10 or 16; any other value resets to 0, which means "no base conversion". |
mask |
Flags such as `std::ios::hex |
Returns: an object of an unspecified type that you pass to << or >>. Do not store it; use it only inline.
Errors / undefined behavior: none for ordinary use. A negative width is treated as zero; very large precisions are accepted but only digits that exist are printed.
Usage notes
- One-shot versus sticky.
setwaffects only the next formatted item and is reset afterwards.setfill,setprecision,setbase,fixed,hex,leftand the rest stay until you change them. Restore the state after changing it, especially in functions that use a caller's stream. - Strings are not truncated by
setw. Items wider than the width print in full. There is no stream manipulator to truncate; usesubstrorstd::formatwith a precision. - Alignment.
std::left,std::rightandstd::internal(sign or prefix on the left, digits on the right) choose where the fill goes. The default is right. setprecisionmeaning changes withfixed. Withoutfixedorscientific, it counts significant digits and trailing zeros are dropped; withfixedit counts decimals and keeps trailing zeros. Reset withunsetf(std::ios::floatfield)orstd::defaultfloat.std::put_time(&tm, "%Y-%m-%d")formats astd::tm; its output depends on the current time and the C locale, so examples that print it are not deterministic.setwwith input.cin >> std::setw(n) >> charArraylimits how many characters are read into a C array, a classic safety measure.- Alternative. The
<format>library does all of this in one string, e.g.std::format("{:>10.2f}", x), with no state left behind.