Stream manipulators

Download c++ eBook

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.

Related Examples

Stats

22 Contributors: 2
Wednesday, July 5, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook