2. Manipulators
● Manipulators are functions specifically designed to be used in
conjunction with the insertion (<<) and extraction (>>)
operators on stream objects.
● Manipulators are used to change formatting parameters on
streams and to insert or extract certain special characters.
● Manipulators are classified into two,
1. Manipulators without arguments
2. Manipulators with Arguments
3. Manipulators without arguments
● endl: It is defined in ostream.
It is used to enter a new line and after entering a new line it flushes.
● ws: It is defined in istream and is used to ignore the whitespaces in the string sequence.
● Ends: It inserts a null character into the output stream. It typically works with std::ostrstream.
● flush: It flushes the output stream i.e. it forces all the output written on the screen or in the file.
4. Manipulators without arguments
int main()
{
istringstream str(" Programmer");
string line;
// Ignore all the whitespace in string
getline(str >> std::ws, line);
cout << line << endl;
cout << "only a test" << flush;
cout << "na";
cout << "b" << ends;
cout << "c" << endl;
return 0;
}
5. Manipulators with arguments
1. setw (val): It is used to sets the field width in output operations.
2. setfill (c): It is used to fill the character ‘c’ on output stream.
3. setprecision (val): It sets val as the new value for the precision of
floating-point values.
4. setbase(val): It is used to set the numeric base value for numeric
values.
5. setiosflags(flag): It is used to sets the format flags specified by
parameter mask.
6. resetiosflags(m): It is used to resets the format flags specified by
parameter mask.
6. Manipulators with arguments
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
double f =3.14159;
cout <<setw(10)<< 77 <<endl;
cout << setprecision(3) << f << 'n';
cout << setfill ('x') << setw (10)<< 77 << endl;
cout << setbase(16)<< 110 << endl;
}
7. Basic format flags
Independent flags (switch on)
boolalpha Alphanumerical bool values.
showbase Show numerical base prefixes.
showpoint Show decimal point.
showpos Show positive signs.
skipws Skip whitespaces.
unitbuf Flush buffer after insertions.
uppercase Generate upper case letters.
8. Independent flags (switch off)
noboolalpha Do alphanumerical bool values.
noshowbase Do not show numerical base prefixes.
noshowpoint Do not show decimal point.
noshowpos Do not show positive signs.
noskipws Do not skip whitespaces.
nounitbuf Do not force flushes after insertions.
nouppercase Do not generate upper case letters.
9. Basic format flags :
*boolalpha & noboolalpha
bool b = true;
cout << boolalpha << b << 'n';
cout << noboolalpha << b << 'n';
*showbase & noshowbase
int n = 20;
cout << hex << showbase << n << 'n';
cout << hex << noshowbase << n << 'n';
Output:
true
1
Output:
0x14
14
10. Basic format flags :
*setpoint & nosetpoint
double a = 30,b = 3.141543;
cout.precision (5);
cout << showpoint << a << 't' << b<<endl;
cout << noshowpoint << a << 't' << b;
*showpos & noshowpos
int p = 1;
int n = -1;
cout << showpos << p << 't' << n << 'n';
cout << noshowpos << p << 't' << n << 'n';
Output:
30.000 3.1415
30 3.1415
Output:
+1 -1
1 -1
11. *skipws & noskipws
char a, b, c;
istringstream iss (" 123");
iss >> skipws >> a >> b >> c;
cout << a << b << c << 'n';
iss.seekg(0);
iss >> noskipws >> a >> b >> c;
cout << a << b << c << 'n';
*uppercase & nouppercase
cout << hex;
cout << uppercase << 77 << 'n';
cout << nouppercase << 77 << 'n';
Output:
123
1
Output:
4D
4d
12. Numerical base format flags ("basefield" flags)
dec Use decimal base
hex Use hexadecimal base
oct Use octal base
Floating point format flags ("floatfield" flags)
fixed Use fixed floating point notation
scientific Use scientific floating point notation
13. Numerical base format flags ("basefield" flags)
*dec & hex & oct
int n = 70;
cout << dec << n << 'n';
cout << hex << n << 'n';
cout << oct << n << 'n';
*fixed & scientific
double a = 3.1415926534;
cout.precision(5);
cout << a <<"n";
cout << fixed << a <<"n" ;
cout << scientific << a <<"n";
Output:
70
46
106
Output:
3.1416
3.14159
3.1415e+000
14. Adjustment format flags ("adjustfileld "flags)
internal Sets field by inserting characters at an internal position.
left Adjust output to the left.
right Adjust output to the right.
15. Adjustment format flags ("adjustfileld "flags)
*internal & left & right
int n = -5;
cout.width(6); cout << internal << n << 'n';
cout.width(6); cout << left << n << 'n';
cout.width(6); cout << right << n << 'n';
Output:
- 5
-5
-5