Stream manipulators
suggest changeManipulators 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:
os.width(n);
equals toos << std::setw(n); is.width(n);
equals tois >> std::setw(n);
os.precision(n);
equals toos << std::setprecision(n); is.precision(n);
equals tois >> std::setprecision(n);
os.setfill(c);
equals toos << std::setfill(c);
str >> std::setbase(base);
orstr << std::setbase(base);
equals tostr.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);
os.setf(std::ios_base::flag);
equals toos << std::flag; is.setf(std::ios_base::flag);
equals tois >> std::flag; os.unsetf(std::ios_base::flag);
equals toos << std::no ## flag; is.unsetf(std::ios_base::flag);
equals tois >> std::no ## flag;
(where ## - is concatenation operator) for nextflag
s:boolalpha
,showbase
,showpoint
,showpos
,skipws
,uppercase
.std::ios_base::basefield
. Forflag
s:dec
,hex
andoct
:os.setf(std::ios_base::flag, std::ios_base::basefield);
equals toos << std::flag; is.setf(std::ios_base::flag, std::ios_base::basefield);
equals tois >> std::flag;
( 1 )str.unsetf(std::ios_base::flag, std::ios_base::basefield);
equals tostr.setf(std::ios_base::fmtflags(0), std::ios_base::basefield);
( 2 )
std::ios_base::adjustfield
. Forflag
s:left
,right
andinternal
:os.setf(std::ios_base::flag, std::ios_base::adjustfield);
equals toos << std::flag; is.setf(std::ios_base::flag, std::ios_base::adjustfield);
equals tois >> std::flag;
( 1 )str.unsetf(std::ios_base::flag, std::ios_base::adjustfield);
equals tostr.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 ) Ifflag
is set.std::ios_base::floatfield
.os.setf(std::ios_base::flag, std::ios_base::floatfield);
equals toos << std::flag; is.setf(std::ios_base::flag, std::ios_base::floatfield);
equals tois >> std::flag;
for
flag
s:fixed
andscientific
.os.setf(std::ios_base::fmtflags(0), std::ios_base::floatfield);
equals toos << std::defaultfloat; is.setf(std::ios_base::fmtflags(0), std::ios_base::floatfield);
equals tois >> std::defaultfloat;
str.setf(std::ios_base::fmtflags(0), std::ios_base::flag);
equals tostr.unsetf(std::ios_base::flag)
forflag
s:basefield
,adjustfield
,floatfield
.os.setf(mask)
equals toos << setiosflags(mask);
is.setf(mask)
equals tois >> setiosflags(mask); os.unsetf(mask)
equals toos << resetiosflags(mask);
is.unsetf(mask)
equals tois >> resetiosflags(mask);
For almost all mask
of std::ios_base::fmtflags
type.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents