Manipulators are used for formatting output. The data is manipulated by the programmer’s choice of display. It doesn’t indicate that the value of a variable is changed; instead, it adjusts the I/O stream using the insertion <<
and extraction >>
operator.
There are several manipulators available in C++. The most commonly used manipulators are discussed below:
Manipulators Without Arguments
The IOStream library provides the most important manipulators, which are described below.
1. endl
The endl
manipulator has the same functionality as the \n
newline character but with the difference of flushing the stream. It is defined in the ostream
.
2. ends
It’s also defined in ostream
, and it adds a null character to the output stream. When the linked output buffer has to be null-terminated to be processed as a C string, it usually works with std::ostrstream
.
3. ws
It is used to remove whitespaces in the string sequence and is defined in istream.
4. flush
It is also defined in ostream
and flushes the output stream, which means it forces all output to be sent to the screen or to a file. The output would be the same if there was no flush, but it might not display in real time.
Manipulators with Arguments
Setw (20)
, setfill ('*')
, and a number of other manipulators are used with the argument. All of this is defined in the header file iomanip
. We must include this header file in our program if we want to use these manipulators.
For example, you can select the minimum width and fill the empty area with any character you like using the manipulators below.
1. setw
On output, this manipulator sets the minimum field width. The syntax of the setw manipulator is:
setw (x)
setw
causes the number or string that comes after it to be printed with a field of x
characters wide, where x
is the argument set in the setw manipulator. <iomanip.h>
is the header file that must be included when using the setw
manipulator.
2. setfill (c)
It’s used to fill the output stream with the character c
.
3. setprecision (val)
It sets val
as the new precision value for floating-point numbers.
4. setiosflags(flag)
It’s used to set the format flags that the parameter mask specifies.
#include <iostream> #include <iomanip> // For manipulators like setw, setfill #include <sstream> // For stringstream using namespace std; int main() { // endl manipulator cout << "Using the endl manipulator:" << endl; cout << "Learning " << endl; cout << "C++ " << endl; cout << "With TechABU.co " << endl; // ends manipulator cout << "\nUsing the ends manipulator:" << endl; cout << "x "; cout << "\ny" << ends; cout << "z" << endl; // ws manipulator cout << "\nUsing the ws manipulator:" << endl; istringstream str(" I'm a Programming Enthusiast"); string line; getline(str >> ws, line); cout << line << endl; // setw manipulator cout << "\nUsing the setw manipulator:" << endl; int a = 200, b = 300; cout << setw(5) << a << setw(5) << b << endl; cout << setw(6) << a << setw(6) << b << endl; cout << setw(7) << a << setw(7) << b << endl; cout << setw(8) << a << setw(8) << b << endl; // setfill manipulator cout << "\nUsing the setfill manipulator:" << endl; cout << setfill('x'); cout << setw(10) << 11 << "\n"; cout << setw(10) << 222 << "\n"; cout << setw(10) << 3333 << "\n"; cout << setw(10) << 44444 << "\n"; return 0; }
OUTPUT OF THE PROGRAM
Using the endl manipulator:
Learning
C++
With TechABU.co
Using the ends manipulator:
x
y
z
Using the ws manipulator:
I'm a Programming Enthusiast
Using the setw manipulator:
200 300
200 300
200 300
200 300
Using the setfill manipulator:
xxxxxxxxx11
xxxxxxxx222
xxxxx3333
xxxx44444