Manipulators in C++ with Examples

Manipulators are helping functions that can modify the input/output stream. It does not mean that we change the value of a variable, it only modifies the I/O stream using insertion (<<) and extraction (>>) operators.

For example, if we want to print the hexadecimal value of 100 then we can print it as:

cout<<setbase(16)<<100

Types of Manipulators



There are various types of manipulators:

  1. Manipulators without arguments: The most important manipulators defined by the IOStream library are provided below.
    • endl: It is defined in ostream. It is used to enter a new line and after entering a new line it flushes (i.e. it forces all the output written on the screen or in the file) the output stream.
    • ws: It is defined in istream and is used to ignore the whitespaces in the string sequence.
    • ends: It is also defined in ostream and it inserts a null character into the output stream. It typically works with std::ostrstream, when the associated output buffer needs to be null-terminated to be processed as a C string.
    • flush: It is also defined in ostream and it flushes the output stream i.e. it forces all the output written on the screen or in the file. Without flush, the output would be the same but may not appear in real-time.

      Examples:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      #include <iostream>
      #include <istream>
      #include <sstream>
      #include <string>
        
      using namespace std;
        
      int main()
      {
          istringstream str("           Programmer");
          string line;
          // Ignore all the whitespace in string
          // str before the first word.
          getline(str >> std::ws, line);
        
          // you can also write str>>ws
          // After printing the output it will automatically
          // write a new line in the output stream.
          cout << line << endl;
        
          // without flush, output will be the same.
          cout << "only a test" << flush;
        
          // Use of ends Manipulator
          cout << "\na";
        
          // NULL character will be added in the Output
          cout << "b" << ends;
          cout << "c" << endl;
        
          return 0;
      }

      chevron_right

      
      

      Output:

      Programmer
      only a test
      abc
      
  2. Manipulators with Arguments: Some of the manipulators are used with the argument like setw (20), setfill (‘*’) and many more. These all are defined in the header file. If we want to use these manipulators then we must include this header file in our program.

    For Example, you can use following manipulators to set minimum width and fill the empty space with any character you want: std::cout << std::setw (6) << std::setfill (’*’);

    • Some important manipulators in <iomanip> are:
      1. setw (val): It is used to sets the field width in output operations.
      2. setfill (c): It is used to fill the character ‘c’ on output stream.
      3. setprecision (val): It sets val as the new value for the precision of floating-point values.
      4. setbase(val): It is used to set the numeric base value for numeric values.
      5. setiosflags(flag): It is used to sets the format flags specified by parameter mask.
      6. resetiosflags(m): It is used to resets the format flags specified by parameter mask.
    • Some important manipulators in <ios> are:
      1. showpos: It forces to show a positive sign on positive numbers.
      2. noshowpos: It forces not to write a positive sign on positive numbers.
      3. showbase: It indicates numeric base of numeric values.
      4. uppercase: It forces uppercase letters for numeric values.
      5. nouppercase: It forces lowercase letters for numeric values.
      6. fixed: It uses decimal notation for ?oating-point values.
      7. scientific: It use scientific floating-point notation.
      8. hex: Read and write hexadecimal values for integers and it works same as the setbase(16).
      9. dec: Read and write decimal values for integers i.e. setbase(10).
      10. oct: Read and write octal values for integers i.e. setbase(10).
      11. left: It adjust output to the left.
      12. right: It adjust output to the right.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iomanip>
    #include <iostream>
    using namespace std;
      
    int main()
    {
        double A = 100;
        double B = 2001.5251;
        double C = 201455.2646;
      
        // We can use setbase(16) here instead of hex
      
        // formatting
        cout << hex << left << showbase << nouppercase;
      
        // actual printed part
        cout << (long long)A << endl;
      
        // We can use dec here instead of setbase(10)
      
        // formatting
        cout << setbase(10) << right << setw(15)
             << setfill('_') << showpos
             << fixed << setprecision(2);
      
        // actual printed part
        cout << B << endl;
      
        // formatting
        cout << scientific << uppercase
             << noshowpos << setprecision(9);
      
        // actual printed part
        cout << C << endl;
    }

    chevron_right

    
    

    Output:

    0x64
    _______+2001.53
    2.014552646E+05
    

References:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.