C++ std::iomanip std::setiosflags


Example

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/O
  • oct - use octal base for integer I/O
  • hex - use hexadecimal base for integer I/O
  • basefield - dec|oct|hex|0 useful for masking operations
  • left - 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 operations
  • scientific - generate floating point types using scientific notation, or hex notation if combined with fixed
  • fixed - generate floating point types using fixed notation, or hex notation if combined with scientific
  • floatfield - scientific|fixed|(scientific|fixed)|0. Useful for masking operations
  • boolalpha - insert and extract bool type in alphanumeric format
  • showbase - generate a prefix indicating the numeric base for integer output, require the currency indicator in monetary I/O
  • showpoint - generate a decimal-point character unconditionally for floating-point number output
  • showpos - generate a + character for non-negative numeric output
  • skipws - skip leading whitespace before certain input operations
  • unitbuf flush the output after each output operation
  • uppercase - replace certain lowercase letters with their uppercase equivalents in certain output output operations

Example 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;
    }