Manipulators in C++ with some examples

In this tutorial, we will learn about Manipulators in C++.

Manipulators are used for changing the output results. They are formatting outputs. It is of two types:

  • Manipulator function. E.g. setfill, setprecision,setiosflag etc.
  • Manipulator operator. E.g. endl, setw, setbase, etc.

And all these manipulators are available in iomanip.h.

Some most common Manipulators in C++ are:

  • setw: Generally in C language, we use \t to give the tab space and tab space means to give more space. But if we want a formatted output for example if we want a table represented in a  formatted output of some specific rows and columns then in these types of cases setw is of much use.  In setw w stands for a width that means it specifies a specific amount of spaces on the screen. And one important thing to note here is that generally in cout and printf the output is print from left to right but in setw, it prints from right to left which is a major advantage in this.

For example:

  • cout<<setw(5) << 1 <<endl;
  • cout<<setw(5) << 1o <<endl;

Here, the first line leaves 5 spaces on the console from left to write and print 1 from right to left on these 5 spaces and second-line print             10 from right to left on these 5 spaces on the 2nd line. So this how it works.

  • setfill: To fill the blank spaces on the console after using setw() we can use setfill function.

For example:

cout<< setw(10) << setfill(‘*’)<<100; // output: *******100

  • setprecision: The setprecision is a predefined function which is a manipulator is used to specify the number of decimal places in floating values.

For example:

  •  cout<< setprecision(2) <<22/7.0;

This line instead of printing 3.142758 print 3.14 because of using setprecsion(2).

  • setbase: It is used to change the number system from one format to another format. Suppose we have a decimal number 65 and we want to change it into octal number then setbase is used here.

For example:

  •  cout<< setbase(8)<<65;  //change the decimal 65 into octal 65 and the output is 101.
  • cout<< setbase(10)<<0101; // change the octal 65 into decimal 65 and the output is 65.
  • cout<< setbase(10)<<0x41; // change the hexadecimal 65 into decimal 65 and the output is 65.

 

  •  setiosflag: Generally it is used to set different types of the flag in our program. That means when we use setw it print the matter from Right to left but names are always print from left to right. So for that, we use setiosflag. Setiosflag also used to represent the sign, to represent the number scientifically and also use it in the number system. We see all its examples in the program below.

For example:

  •   cout<< setw(10) << setiosflag(ios::left)<<“Hello”<<endl; //output is Hello.
#include<iomanip>
#include<iostream>
using namespace std;
int main()
{
  cout<< setw(10) << 1 << endl;
  cout<< setw(10) << 10 << endl;
  cout<< setw(10) << setfill('*')<< 100 << endl;
  cout<< setprecision(2) << 22/7.0 << endl;
  cout<< setbase(8) << 65 << endl;
  cout<< setbase(10) << 0101 << endl;
  cout<< setbase(10) << 0x41 << endl;
  cout<< setw(5) << setiosflags(ios::left)<<"Hello"<< endl;
}

Output:

         1

        10

*******100

3.14

101

65

65

Hello

 

I  hope you all understand it better.

Also read: std::bitset::bitset in C++

Leave a Reply

Your email address will not be published. Required fields are marked *