Stream manipulators

suggest change

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:
    • 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 )
  7. 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.

  8. 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;
  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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents