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 many ways. For example:

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

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

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

  4. 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);
    
  5. 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.

  6. std::ios_base::basefield. For flags: dec, hex and oct:

  7. std::ios_base::adjustfield. For flags: left, right and internal:

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

  8. std::ios_base::floatfield.

    for flags: fixed and scientific.

  9. str.setf(std::ios_base::fmtflags(0), std::ios_base::flag); equals to str.unsetf(std::ios_base::flag) for flags: basefield, adjustfield, floatfield.

  10. 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

Output stream manipulators

Input stream manipulators