Manipulators in C++

Manipulators in C++

Manipulators are special functions used to modify the input/output streams. They are used in conjunction with the insertion (<<) and extraction (>>). Here are some of the widely used manipulators in C++ :

1. endl –
It works the same as “\n”. It is used to enter a new line and flushes the output stream.
Example:

#include<iostream>
using namespace std;
int main()
{
    cout<<"abc"<<endl<<"xyz";
    return 0;
}

Output
abc
xyz

2. ends –
It inserts a null character into the output stream.
Example:

#include<iostream>
using namespace std;
int main()
{
    cout<<"abc"<<ends<<"xyz";
    return 0;
}

Output abc xyz

3. flush –
It flushes the stream buffer and is defined in ostream.

4. ws –
It eliminates whitespace characters in input stream.
Example:

#include<iostream>
#include<sstream>
using namespace std;
int main()
{
    string a,b,c;
    istringstream iss ("red        blue    yellow");
    iss >> a >> ws >> b >> c;
    cout << a << b << c;
    return 0;
}

Output- redblueyellow

5. setprecision –
It changes floating-point precision. We can choose how many digits of decimal value to display. This manipulator is declared in <iomanip> library.
Example:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    double pi = 3.14159265359;
    cout << setprecision(3) << pi << endl;
    cout << setprecision(8) << pi;
    return 0;
}

Output-
3.14
3.1415927

6. setw –
It sets the field width for output operations. This manipulator is declared in <iomanip> library.
Example:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int n = 1;
    while(n <= 10000)
    {
        cout << setw(5) << n <<endl;
        n *= 10;
    }
    return 0;
}

Output
1
10
100
1000
10000

7. setfill –
It sets fill character for the specified field width. This manipulator is declared in <iomanip> library.
Example:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int n = 1;
    while(n <= 10000)
    {
        cout << setw(5) << setfill('x') << n <<endl;
        n *= 10;
    }
    return 0;
}

Output
xxxx1
xxx10
xx100
x1000
10000

8. left
It adjusts output to the left. The output is padded to the field width appending fill characters at the end. By default fill characters will be null character ‘ ‘.
9. right –
It adjusts output to the right. The output is padded to the field width appending fill characters at the end. By default fill characters will be null character ‘ ‘.
Example:

#include<iostream>
using namespace std;
int main()
{
    cout.width(15);
    cout<<left<<"Hello\n";
    cout<<right<<"World";
    return 0;
}

Output
Hello
    World

10. oct, dec, hex, setbase –They are used to set the numeric base value for numbers. setbase has parameter 8 for octal, 10 for decimal, 16 for hexadecimal. oct, dec, hex can be used directly too. Both ways output is the same.
Example:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    cout << oct << 1234 << endl;
    cout << dec << 1234 << endl;
    cout << hex << 1234 << endl;
    cout << setbase(8) << 1234 << endl;
    cout << setbase(10) << 1234 << endl;
    cout << setbase(16) << 1234 << endl;
    return 0;
}

Output-
2322
1234
4d2
2322
1234
4d2

Conclusion-

This was the concept of Manipulators in C++. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

For daily updates do follow us on Instagram. Visit again!

ErrorFreeProgram.

3 COMMENTS

Leave a Reply

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