What are Manipulators?

0 votes
122 views
Shikhar Arora asked 06-Dec-2019 in Technology by Shikhar Arora

What are Manipulators

1 Answer

0 votes
Rahul Roi answered 29 days ago by Rahul Roi

The Manipulators in C++

The Manipulators are helping functions that can modify the input/output stream. Here, 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

The types of Manipulators

These are various types of manipulators:

(1). Manipulators without arguments

  • endl
  • ws
  • ends
  • flush

(2). Manipulators with Arguments

Some important manipulators in <iomanip> are

  • setw (val)
  • setfill (c)
  • setprecision (val)
  • setbase(val)
  • setiosflags(flag)
  • resetiosflags(m)

Some important manipulators in <ios> are

  • showpos
  • noshowpos
  • showbase
  • uppercase
  • nouppercase
  • fixed
  • scientific
  • hex
  • dec
  • oct
  • left
  • right
For Testing :-
#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, the 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;

Output

Programmer

only a test
abc


Read More :- MindStick Forum