C++

Stream manipulators

Introduction#

Manipulators are special helper functions that help controlling input and output streams using operator >> or operator <<.

They all can be included by #include <iomanip>.

Remarks#

Manipulators can be used in other way. For example:

  1. os.width(n); equals to os << std::setw(n);

is.width(n); equals to is >> std::setw(n);

  1. os.precision(n); equals to os << std::setprecision(n);

is.precision(n); equals to is >> std::setprecision(n);

  1. os.setfill(c); equals to os << std::setfill(c);

  1. str >> std::setbase(base); or str << std::setbase(base);

equals to

str.setf(base ==  8 ? std::ios_base::oct :
            base == 10 ? std::ios_base::dec :
                base == 16 ? std::ios_base::hex :
                     std::ios_base::fmtflags(0),
         std::ios_base::basefield);

  1. os.setf(std::ios_base::flag); equals to os << std::flag;

is.setf(std::ios_base::flag); equals to is >> std::flag;

os.unsetf(std::ios_base::flag); equals to os << std::no ## flag;
is.unsetf(std::ios_base::flag); equals to is >> std::no ## flag;

(where ## - is concatenation operator)

for next flags: boolalpha, showbase, showpoint, showpos, skipws, uppercase.

  1. std::ios_base::basefield.

For flags: dec, hex and oct:

  • os.setf(std::ios_base::flag, std::ios_base::basefield); equals to os << std::flag;

is.setf(std::ios_base::flag, std::ios_base::basefield); equals to is >> std::flag;
( 1 )

  • str.unsetf(std::ios_base::flag, std::ios_base::basefield); equals to str.setf(std::ios_base::fmtflags(0), std::ios_base::basefield);

( 2 )

  1. std::ios_base::adjustfield.

For flags: left, right and internal:

  • os.setf(std::ios_base::flag, std::ios_base::adjustfield); equals to os << std::flag;

is.setf(std::ios_base::flag, std::ios_base::adjustfield); equals to is >> std::flag;
( 1 )

  • str.unsetf(std::ios_base::flag, std::ios_base::adjustfield); equals to str.setf(std::ios_base::fmtflags(0), std::ios_base::adjustfield);

( 2 )

( 1 ) If flag of corresponding field previously set have already unset by unsetf.
( 2 ) If flag is set.

  1. std::ios_base::floatfield.
  • os.setf(std::ios_base::flag, std::ios_base::floatfield); equals to os << std::flag;

is.setf(std::ios_base::flag, std::ios_base::floatfield); equals to is >> std::flag;

for flags: fixed and scientific.

  • os.setf(std::ios_base::fmtflags(0), std::ios_base::floatfield); equals to os << std::defaultfloat;

is.setf(std::ios_base::fmtflags(0), std::ios_base::floatfield); equals to is >> std::defaultfloat;

  1. str.setf(std::ios_base::fmtflags(0), std::ios_base::flag); equals to str.unsetf(std::ios_base::flag)

for flags: basefield, adjustfield, floatfield.

  1. os.setf(mask) equals to os << setiosflags(mask);

is.setf(mask) equals to is >> setiosflags(mask);

os.unsetf(mask) equals to os << resetiosflags(mask);
is.unsetf(mask) equals to is >> resetiosflags(mask);

For almost all mask of std::ios_base::fmtflags type.

Stream manipulators

std::boolalpha and std::noboolalpha - switch between textual and numeric representation of booleans.

std::cout << std::boolalpha << 1;
// Output: true

std::cout << std::noboolalpha << false;
// Output: 0

bool boolValue;
std::cin >> std::boolalpha >> boolValue;
std::cout << "Value \"" << std::boolalpha << boolValue
          << "\" was parsed as " << std::noboolalpha << boolValue;
// Input: true
// Output: Value "true" was parsed as 0

std::showbase and std::noshowbase - control whether prefix indicating numeric base is used.

std::dec (decimal), std::hex (hexadecimal) and std::oct (octal) - are used for changing base for integers.

#include <sstream>

std::cout << std::dec << 29 << ' - '
          << std::hex << 29 << ' - '
          << std::showbase << std::oct << 29 << ' - '
          << std::noshowbase << 29  '\n';
int number;
std::istringstream("3B") >> std::hex >> number;
std::cout << std::dec << 10;
// Output: 22 - 1D - 35 - 035
// 59

Default values are std::ios_base::noshowbase and std::ios_base::dec.

If you want to see more about std::istringstream check out the <sstream> header.


std::uppercase and std::nouppercase - control whether uppercase characters are used in floating-point and hexadecimal integer output. Have no effect on input streams.

std::cout << std::hex << std::showbase
              << "0x2a with nouppercase: " << std::nouppercase << 0x2a << '\n'
              << "1e-10 with uppercase: " << std::uppercase << 1e-10 << '\n'
}
// Output: 0x2a with nouppercase: 0x2a
// 1e-10 with uppercase: 1E-10

Default is std::nouppercase.


std::setw(n) - changes the width of the next input/output field to exactly n.

The width property n is resetting to 0 when some functions are called (full list is here).

std::cout << "no setw:" << 51 << '\n'
          << "setw(7): " << std::setw(7) << 51 << '\n'
          << "setw(7), more output: " << 13
          << std::setw(7) << std::setfill('*') << 67 << ' ' << 94 << '\n';

char* input = "Hello, world!";
char arr[10];
std::cin >> std::setw(6) >> arr;
std::cout << "Input from \"Hello, world!\" with setw(6) gave \"" << arr << "\"\n";

// Output: 51
// setw(7):      51
// setw(7), more output: 13*****67 94

// Input: Hello, world!
// Output: Input from "Hello, world!" with setw(6) gave "Hello"

Default is std::setw(0).


std::left, std::right and std::internal - modify the default position of the fill characters by setting std::ios_base::adjustfield to std::ios_base::left, std::ios_base::right and std::ios_base::internal correspondingly. std::left and std::right apply to any output, std::internal - for integer, floating-point and monetary output. Have no effect on input streams.

#include <locale>
...

std::cout.imbue(std::locale("en_US.utf8"));

std::cout << std::left << std::showbase << std::setfill('*')
          << "flt: " << std::setw(15) << -9.87  << '\n'
          << "hex: " << std::setw(15) << 41 << '\n'
          << "  $: " << std::setw(15) << std::put_money(367, false) << '\n'
          << "usd: " << std::setw(15) << std::put_money(367, true) << '\n'
          << "usd: " << std::setw(15)
          << std::setfill(' ') << std::put_money(367, false) << '\n';
// Output:
// flt: -9.87**********
// hex: 41*************
//   $: $3.67**********
// usd: USD *3.67******
// usd: $3.67          

std::cout << std::internal << std::showbase << std::setfill('*')
          << "flt: " << std::setw(15) << -9.87  << '\n'
          << "hex: " << std::setw(15) << 41 << '\n'
          << "  $: " << std::setw(15) << std::put_money(367, false) << '\n'
          << "usd: " << std::setw(15) << std::put_money(367, true) << '\n'
          << "usd: " << std::setw(15)
          << std::setfill(' ') << std::put_money(367, true) << '\n';
// Output:
// flt: -**********9.87
// hex: *************41
//   $: $3.67**********
// usd: USD *******3.67
// usd: USD        3.67

std::cout << std::right << std::showbase << std::setfill('*')
          << "flt: " << std::setw(15) << -9.87  << '\n'
          << "hex: " << std::setw(15) << 41 << '\n'
          << "  $: " << std::setw(15) << std::put_money(367, false) << '\n'
          << "usd: " << std::setw(15) << std::put_money(367, true) << '\n'
          << "usd: " << std::setw(15)
          << std::setfill(' ') << std::put_money(367, true) << '\n';
// Output:
// flt: **********-9.87
// hex: *************41
//   $: **********$3.67
// usd: ******USD *3.67
// usd:       USD  3.67

Default is std::left.


std::fixed, std::scientific, std::hexfloat [C++11] and std::defaultfloat [C++11] - change formatting for floating-point input/output.

std::fixed sets the std::ios_base::floatfield to std::ios_base::fixed,
std::scientific - to std::ios_base::scientific,
std::hexfloat - to std::ios_base::fixed | std::ios_base::scientific and
std::defaultfloat - to std::ios_base::fmtflags(0).

fmtflags

#include <sstream>
...

std::cout << '\n'
          << "The number 0.07 in fixed:      " << std::fixed << 0.01 << '\n'
          << "The number 0.07 in scientific: " << std::scientific << 0.01 << '\n'
          << "The number 0.07 in hexfloat:   " << std::hexfloat << 0.01 << '\n'
          << "The number 0.07 in default:    " << std::defaultfloat << 0.01 << '\n';

double f;
std::istringstream is("0x1P-1022");
double f = std::strtod(is.str().c_str(), NULL);
std::cout << "Parsing 0x1P-1022 as hex gives " << f << '\n';

// Output:
// The number 0.01 in fixed:      0.070000
// The number 0.01 in scientific: 7.000000e-02
// The number 0.01 in hexfloat:   0x1.1eb851eb851ecp-4
// The number 0.01 in default:    0.07
// Parsing 0x1P-1022 as hex gives 2.22507e-308

Default is std::ios_base::fmtflags(0).

There is a bug on some compilers which causes

double f;
std::istringstream("0x1P-1022") >> std::hexfloat >> f;
std::cout << "Parsing 0x1P-1022 as hex gives " << f << '\n';
// Output: Parsing 0x1P-1022 as hex gives 0

std::showpoint and std::noshowpoint - control whether decimal point is always included in floating-point representation. Have no effect on input streams.

std::cout << "7.0 with showpoint: " << std::showpoint << 7.0 << '\n'
          << "7.0 with noshowpoint: " << std::noshowpoint << 7.0 << '\n';
// Output: 1.0 with showpoint: 7.00000
// 1.0 with noshowpoint: 7

Default is std::showpoint.


std::showpos and std::noshowpos - control displaying of the + sign in non-negative output. Have no effect on input streams.

std::cout << "With showpos: " << std::showpos
          << 0 << ' ' << -2.718 << ' ' << 17 << '\n'
          << "Without showpos: " << std::noshowpos
          << 0 << ' ' << -2.718 << ' ' << 17 << '\n';
// Output: With showpos: +0 -2.718 +17
// Without showpos: 0 -2.718 17

Default if std::noshowpos.


std::unitbuf, std::nounitbuf - control flushing output stream after every operation. Have no effect on input stream. std::unitbuf causes flushing.


std::setbase(base) - sets the numeric base of the stream.

std::setbase(8) equals to setting std::ios_base::basefield to std::ios_base::oct,
std::setbase(16) - to std::ios_base::hex,
std::setbase(10) - to std::ios_base::dec.

If base is other then 8, 10 or 16 then std::ios_base::basefield is setting to std::ios_base::fmtflags(0). It means decimal output and prefix-dependent input.

As default std::ios_base::basefield is std::ios_base::dec then by default std::setbase(10).


std::setprecision(n) - changes floating-point precision.

#include <cmath>
#include <limits>
...

typedef std::numeric_limits<long double> ld;
const long double pi = std::acos(-1.L);

std::cout << '\n'
          << "default precision (6):   pi: " << pi << '\n'
          << "                       10pi: " << 10 * pi << '\n'
          << "std::setprecision(4):  10pi: " << std::setprecision(4) << 10 * pi << '\n'
          << "                    10000pi: " << 10000 * pi << '\n'
          << "std::fixed:         10000pi: " << std::fixed << 10000 * pi << std::defaultfloat << '\n'
          << "std::setprecision(10):   pi: " << std::setprecision(10) << pi << '\n'
          << "max-1 radix precicion:   pi: " << std::setprecision(ld::digits - 1) << pi << '\n'
          << "max+1 radix precision:   pi: " << std::setprecision(ld::digits + 1) << pi << '\n'
          << "significant digits prec: pi: " << std::setprecision(ld::digits10) << pi << '\n';

// Output:
// default precision (6):   pi: 3.14159
//                        10pi: 31.4159
// std::setprecision(4):  10pi: 31.42
//                     10000pi: 3.142e+04
// std::fixed:         10000pi: 31415.9265
// std::setprecision(10):   pi: 3.141592654
// max-1 radix precicion:   pi: 3.14159265358979323851280895940618620443274267017841339111328125
// max+1 radix precision:   pi: 3.14159265358979323851280895940618620443274267017841339111328125
// significant digits prec: pi: 3.14159265358979324

Default is std::setprecision(6).


std::setiosflags(mask) and std::resetiosflags(mask) - set and clear flags specified in mask of std::ios_base::fmtflags type.

#include <sstream>
...

std::istringstream in("10 010 10 010 10 010");
int num1, num2;

in >> std::oct >> num1 >> num2;
std::cout << "Parsing \"10 010\" with std::oct gives:   " << num1 << ' ' << num2 << '\n';
// Output: Parsing "10 010" with std::oct gives:   8 8

in >> std::dec >> num1 >> num2;
std::cout << "Parsing \"10 010\" with std::dec gives:   " << num1 << ' ' << num2 << '\n';
// Output: Parsing "10 010" with std::oct gives:   10 10

in >> std::resetiosflags(std::ios_base::basefield) >> num1 >> num2;
std::cout << "Parsing \"10 010\" with autodetect gives: " << num1 << ' ' << num2 << '\n';
// Parsing "10 010" with autodetect gives: 10 8

std::cout << std::setiosflags(std::ios_base::hex |
                              std::ios_base::uppercase |
                              std::ios_base::showbase) << 42 << '\n';
// Output: OX2A

std::skipws and std::noskipws - control skipping of leading whitespace by the formatted input functions. Have no effect on output streams.

#include <sstream>
...

char c1, c2, c3;
std::istringstream("a b c") >> c1 >> c2 >> c3;
std::cout << "Default  behavior:  c1 = " << c1 << "  c2 = " << c2 << "  c3 = " << c3 << '\n';

std::istringstream("a b c") >> std::noskipws >> c1 >> c2 >> c3;
std::cout << "noskipws behavior:  c1 = " << c1 << "  c2 = " << c2 << "  c3 = " << c3 << '\n';
// Output: Default  behavior:  c1 = a  c2 = b  c3 = c
// noskipws behavior:  c1 = a  c2 =    c3 = b

Default is std::ios_base::skipws.


std::quoted(s[, delim[, escape]]) [C++14] - inserts or extracts quoted strings with embedded spaces.

s - the string to insert or extract.
delim - the character to use as the delimiter, " by default.
escape - the character to use as the escape character, \ by default.

#include <sstream>
...
 
std::stringstream ss;
std::string in = "String with spaces, and embedded \"quotes\" too";
std::string out;
 
ss << std::quoted(in);
std::cout << "read in     [" << in << "]\n"
          << "stored as   [" << ss.str() << "]\n";
 
ss >> std::quoted(out);
std::cout << "written out [" << out << "]\n";
// Output:
// read in     [String with spaces, and embedded "quotes" too]
// stored as   ["String with spaces, and embedded \"quotes\" too"]
// written out [String with spaces, and embedded "quotes" too]

For more information see the link above.

Output stream manipulators

std::ends - inserts a null character '\0' to output stream. More formally this manipulator’s declaration looks like

template <class charT, class traits>
std::basic_ostream<charT, traits>& ends(std::basic_ostream<charT, traits>& os);

and this manipulator places character by calling os.put(charT()) when used in an expression
os << std::ends;


std::endl and std::flush both flush output stream out by calling out.flush(). It causes immediately producing output. But std::endl inserts end of line '\n' symbol before flushing.

std::cout << "First line." << std::endl << "Second line. " << std::flush
          << "Still second line.";
// Output: First line.
// Second line. Still second line.

std::setfill(c) - changes the fill character to c. Often used with std::setw.

std::cout << "\nDefault fill: " << std::setw(10) << 79 << '\n'
          << "setfill('#'): " << std::setfill('#')
          << std::setw(10) << 42 << '\n';
// Output:
// Default fill:         79
// setfill('#'): ########79

std::put_money(mon[, intl]) [C++11]. In an expression out << std::put_money(mon, intl), converts the monetary value mon (of long double or std::basic_string type) to its character representation as specified by the std::money_put facet of the locale currently imbued in out. Use international currency strings if intl is true, use currency symbols otherwise.

long double money = 123.45;
// or std::string money = "123.45";
 
std::cout.imbue(std::locale("en_US.utf8"));
std::cout << std::showbase << "en_US: " << std::put_money(money)
          << " or " << std::put_money(money, true) << '\n';
// Output: en_US: $1.23 or USD  1.23
 
std::cout.imbue(std::locale("ru_RU.utf8"));
std::cout << "ru_RU: " << std::put_money(money)
          << " or " << std::put_money(money, true) << '\n';
// Output: ru_RU: 1.23 руб or 1.23 RUB 
 
std::cout.imbue(std::locale("ja_JP.utf8"));
std::cout << "ja_JP: " << std::put_money(money)
          << " or " << std::put_money(money, true) << '\n';
// Output: ja_JP: ¥123 or JPY  123

std::put_time(tmb, fmt) [C++11] - formats and outputs a date/time value to std::tm according to the specified format fmt.

tmb - pointer to the calendar time structure const std::tm* as obtained from localtime() or gmtime().
fmt - pointer to a null-terminated string const CharT* specifying the format of conversion.

#include <ctime>
...

std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);

std::cout.imbue(std::locale("ru_RU.utf8"));
std::cout << "\nru_RU: " << std::put_time(&tm, "%c %Z") << '\n';
// Possible output:
// ru_RU: Вт 04 июл 2017 15:08:35 UTC

For more information see the link above.

Input stream manipulators

std::ws - consumes leading whitespaces in input stream. It different from std::skipws.

#include <sstream>
...

std::string str;
std::istringstream("  \v\n\r\t    Wow!There   is no whitespaces!") >> std::ws >> str;
std::cout << str;
// Output: Wow!There   is no whitespaces!

std::get_money(mon[, intl]) [C++11]. In an expression in >> std::get_money(mon, intl) parses the character input as a monetary value, as specified by the std::money_get facet of the locale currently imbued in in, and stores the value in mon (of long double or std::basic_string type). Manipulator expects required international currency strings if intl is true, expects optional currency symbols otherwise.

#include <sstream>
#include <locale>
...
 
std::istringstream in("$1,234.56 2.22 USD  3.33");
long double v1, v2;
std::string v3;

in.imbue(std::locale("en_US.UTF-8"));
in >> std::get_money(v1) >> std::get_money(v2) >> std::get_money(v3, true);
if (in) {
    std::cout << std::quoted(in.str()) << " parsed as: "
              << v1 << ", " << v2 << ", " << v3 << '\n';
}
// Output:
// "$1,234.56 2.22 USD  3.33" parsed as: 123456, 222, 333

std::get_time(tmb, fmt) [C++11] - parses a date/time value stored in tmb of specified format fmt.

tmb - valid pointer to the const std::tm* object where the result will be stored.
fmt - pointer to a null-terminated string const CharT* specifying the conversion format.

#include <sstream>
#include <locale>
...

std::tm t = {};
std::istringstream ss("2011-Februar-18 23:12:34");

ss.imbue(std::locale("de_DE.utf-8"));
ss >> std::get_time(&t, "%Y-%b-%d %H:%M:%S");
if (ss.fail()) {
    std::cout << "Parse failed\n";
}
else {
    std::cout << std::put_time(&t, "%c") << '\n';
}
// Possible output:
// Sun Feb 18 23:12:34 2011

For more information see the link above.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow