Data Manipulators
Data Manipulators
Data Manipulators
Manipulators are operators/function/keywords used in C++ for formatting output.
The data is manipulated by the programmer’s choice of display.
Manipulators are helping functions that can modify the input/output stream. It
does not mean that we change the value of a variable, it only modifies the I/O
stream using insertion (<<) and extraction (>>) operators.
Manipulators are functions and keywords specifically designed to be used in
conjunction with the insertion (<<) and extraction (>>) operators .
We have to use <iomanip.h> header files for manipulators.
7. flush:
It is also defined in ostream and it flushes the output stream, i.e. it forces all the output written on the screen or in
the file. Without flush, the output would be the same, but may not appear in real-time. The flush manipulator does
not insert a newline character into the stream before it flushes the buffer.
Ex: cout.flush();
Data Manipulators
8. ios::left: The left() method of stream manipulators in C++ is used to set the adjustfield format flag for the specified str
stream. This flag sets the adjustfield to left. It means that the number in the output will be padded to the field width by inserting
fill characters at the end. its used with setiosflags().
Synatx: setiosflags(ios::left);
Ex: cout<<setiosflags(ios::left)<<value;
9. ios::right: : The right() method of stream manipulators in C++ is used to set the adjustfield format flag for the specified str
stream. This flag sets the adjustfield to right. It means that the number in the output will be padded to the field width by inserting
fill characters at the starting. its used with setiosflags().
Synatx: setiosflags(ios::right);
Ex: cout<<setiosflags(ios::right)<<value;
10. ios::showpos: Sets the showpos format flag for the str stream.
When the showpos format flag is set, a plus sign (+) precedes every non-negative numerical value inserted into the stream.
Synatx: setiosflags(ios::showpos);
Ex: cout<<setiosflags(ios::showpos)<<value;
Data Manipulators
9.ends: C++ manipulator ends function is used to insert a null terminating character on os. The ends manipulator does not take
any argument whenever it is invoked. It causes a null character to the output.
Ex: cout << "a";
cout << "b" << ends;
cout << "c" << endl;
Output: ab c
Data Manipulators
1. Manipulators without arguments:
• endl
• ends
• ws
• flush()
• setiosflags():
1. ios::showpos
2. ios::left
3. ios::right
2. Manipulators with arguments:
setw()
setprecision()
setfill()
setbase()