Manipulators in c++

AshokRaj96 1,731 views 15 slides Mar 03, 2020
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation

Manipulators are functions specifically designed to be used in conjunction with the insertion (>) operators on stream objects


Slide Content

Manipulators in C++

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, Manipulators without arguments Manipulators with Arguments

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.

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; }

Manipulators with arguments setw (val): It is used to sets the field width in output operations. setfill (c): It is used to fill the character ā€˜c’ on output stream. setprecision (val): It sets val as the new value for the precision of floating-point values. setbase(val): It is used to set the numeric base value for numeric values. setiosflags(flag): It is used to sets the format flags specified by parameter mask. resetiosflags(m): It is used to resets the format flags specified by parameter mask.

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; }

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.

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.

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

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

*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

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

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

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.

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