C++ Manipulators

What is the Manipulators in C++?

Manipulators are special functions that are designed to work with the inserter (<< ) and extractor ( >> ) operators in conjunction with one of the I/O stream objects.

There are different manipulators in C++ language like endl, dec, hex, oct, right, left, fixed, scientific, setw(int width), and setfill(char fill)

Example of  endl

#include <iostream>
using namespace std;
int main()
{
        cout << "welcome to C++";
        cout << endl;
        cout << "There are following options" << endl;
        cout << endl;
        cout << endl << "Programming" << endl;
        cout << "JAVA" << endl << "C++" << endl;
        return 0;
}

Output








Explain:
The endl manipulator inserts a new line into the output stream and flushes the output buffer. endl may used anywhere in output statement.

Example of dec, Oct, Hex


#include<iostream>
using namespace std;
int main()

{
        cout << 4308 << endl;    // decimal (base 10) output
        cout << dec << 4308 << endl;  // decimal (base 10) output
        cout << oct << 4308 << endl; // octal (base 8) output
        cout << hex << 4308 << endl;  // hex (base 16) output
        return 0;
}

Output








Explain:
C++ provides three manipulators, decoct, and hex, that format numbers in different bases for output.

Note the following:
       The default output is decimal or base 10
       Order is important: hex << number not number << hex
       All three manipulators change one of the formatting flags; once changed, all numerical output will be in the new base until the base is changed by another manipulator or function


Example osetw, left, right


#include <iostream>
#include <iomanip> //header file for setw,left,right
using namespace std;
int main()
{
   cout << "|" << "Database" << "|" << endl;
   cout << "|" << setw(30) << "Database" << "|" << endl;
   cout << "|" << left << setw(30) << "Database" << "|" << endl;
   cout << "|" << right << setw(30) << "Database" << "|" << endl;
   return 0;
}

Output







Explain:
The following program demonstrates the behavior of three manipulators: setwleft, and right. '|' character at the beginning and ending of each line for clear example.

Example of Fixed, scientific 


#include <iostream>
using namespace std;
int main()
{
        cout << 999999999.55 << endl;
        cout << fixed << 1000000000.60 << endl;
        cout << scientific << 1000000000.60 << endl;
        return 0;
}

Output:








Explain:
Large floating-point numbers are printed in scientific notation by default; small floating-point numbers can be forced to print in scientific notation.

Example of setfill


#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout <<setfill('*');
cout << "|" << "Database" << "|" << endl;
cout << "|" << setw(30) << "Database" << "|" << endl;
cout << "|" << left << setw(30) << "Database" << "|" << endl;
cout << "|" << right << setw(30) << "Database" << "|" << endl;
        return 0;
}

Output:








Explain:
It is used for filled setw empty space with specific character. The default fill character is a blank or space. The setfill function changes the fill character, and once the fill character is changed, it is used fill operations until it is changed with another character.