When used in an expression out << setiosflags(mask)
or in >> setiosflags(mask)
, sets all format flags of the stream out or in as specified by the mask.
List of all std::ios_base::fmtflags
:
dec
- use decimal base for integer I/Ooct
- use octal base for integer I/Ohex
- use hexadecimal base for integer I/Obasefield
- dec|oct|hex|0
useful for masking operationsleft
- left adjustment(add fill characters to the right)right
- right adjustment (adds fill characters to the left)internal
- internal adjustment (adds fill characters to the internal designated point)adjustfield
- left|right|internal
. Useful for masking operationsscientific
- generate floating point types using scientific notation, or hex notation if combined with fixedfixed
- generate floating point types using fixed notation, or hex notation if combined with scientificfloatfield
- scientific|fixed|(scientific|fixed)|0
. Useful for masking operationsboolalpha
- insert and extract bool
type in alphanumeric formatshowbase
- generate a prefix indicating the numeric base for integer output, require the currency indicator in monetary I/Oshowpoint
- generate a decimal-point character unconditionally for floating-point number outputshowpos
- generate a +
character for non-negative numeric outputskipws
- skip leading whitespace before certain input operationsunitbuf
flush the output after each output operationuppercase
- replace certain lowercase letters with their uppercase
equivalents in certain output output operationsExample of manipulators:
#include <iostream>
#include <string>
#include<iomanip>
int main()
{
int l_iTemp = 47;
std::cout<< std::resetiosflags(std::ios_base::basefield);
std::cout<<std::setiosflags( std::ios_base::oct)<<l_iTemp<<std::endl;
//output: 57
std::cout<< std::resetiosflags(std::ios_base::basefield);
std::cout<<std::setiosflags( std::ios_base::hex)<<l_iTemp<<std::endl;
//output: 2f
std::cout<<std::setiosflags( std::ios_base::uppercase)<<l_iTemp<<std::endl;
//output 2F
std::cout<<std::setfill('0')<<std::setw(12);
std::cout<<std::resetiosflags(std::ios_base::uppercase);
std::cout<<std::setiosflags( std::ios_base::right)<<l_iTemp<<std::endl;
//output: 00000000002f
std::cout<<std::resetiosflags(std::ios_base::basefield|std::ios_base::adjustfield);
std::cout<<std::setfill('.')<<std::setw(10);
std::cout<<std::setiosflags( std::ios_base::left)<<l_iTemp<<std::endl;
//output: 47........
std::cout<<std::resetiosflags(std::ios_base::adjustfield)<<std::setfill('#');
std::cout<<std::setiosflags(std::ios_base::internal|std::ios_base::showpos);
std::cout<<std::setw(10)<<l_iTemp<<std::endl;
//output +#######47
double l_dTemp = -1.2;
double pi = 3.14159265359;
std::cout<<pi<<" "<<l_dTemp<<std::endl;
//output +3.14159 -1.2
std::cout<<std::setiosflags(std::ios_base::showpoint)<<l_dTemp<<std::endl;
//output -1.20000
std::cout<<setiosflags(std::ios_base::scientific)<<pi<<std::endl;
//output: +3.141593e+00
std::cout<<std::resetiosflags(std::ios_base::floatfield);
std::cout<<setiosflags(std::ios_base::fixed)<<pi<<std::endl;
//output: +3.141593
bool b = true;
std::cout<<std::setiosflags(std::ios_base::unitbuf|std::ios_base::boolalpha)<<b;
//output: true
return 0;
}