TYPES OF MANIPULATORS IN C++
INTRODUCTION:
To change the format parameters of a stream, manipulators are specialised functions that may be added to the I/O statement. Imani should be included in the programme in order to access manipulators. The list of manipulators in C++ as well as their functions and examples are provided in this article.
What are Manipulators in C++?
- Simple instructions that change the output in different ways are known as manipulators. In other terms, we may say that operators who format the data presentation are known as manipulators.
- The input/output stream can be altered via manipulators, which are supportive features. The I/O stream is solely modified by using the insertion () and extraction (>>) operators, not the value of any variables.
- To change the format parameters of a stream, manipulators are specialised functions that may be added to the I/O statement.
- The format of the data presentation is done by manipulators, which are operators.
- Iomanip.h should be included in the programme in order to access manipulators.
- The hexadecimal number of 100, for instance, can be printed as follows:
example: cout<<setbase(16)<<100
Advantages and Purpose of Manipulators
1.It mostly serves to create the programme framework.2. Special stream functions called manipulators modify the format and features of the input and output.3. Iomanip.h must be included in order to perform manipulator operations.4. In order to employ the insertion () and extraction (>>) operators on stream objects, manipulators functions were specifically created for this purpose.5. Manipulators are employed to alter the format of stream parameters and to insert or remove specific special characters.
Types of Manipulators in C++
They are usually of two types, one accepting argument and the other not.
1. Non-argument manipulators(Without Parameters):
The term "Parameterized manipulators" is also used to describe non-argument manipulators. These manipulators need an iomanip header. The terms setprecision, setw, and setfill are examples:
- Without-arguments manipulators: The most significant manipulators listed in the Iostream library are listed below.·
- It's defined in iostream, endl. It is used to enter a new line, and after doing so, it flushes the output stream, which requires all output to be recorded to the screen or the file.·
- It is used to disregard whitespace in a string sequence and is defined in the istream library as ws.·
- ends: This function, which is defined in ostream, inserts a null character into the output stream. Usually, std:: is compatible with it.when processing the related output buffer as a C string requires that the buffer be null-terminated, use ostrstream.·
- The word "flush" is also used in the ostream programming language. It requires the output stream to be flushed.
Example 1:
#include <iostream> #include <istream> #include <sstream> #include <string> using namespace std; int main() { istringstream str(" Programmer"); string line; getline(str >> std::ws, line); cout << line << endl cout << "only a test" << flush; cout << "\na" cout << "b" << ends; cout << "c" << endl; return 0; }
Output:
Programmer only a test abc
2. Argumented manipulators(With parameters):
Manipulators with Arguments: Setw (20), setfill ('*'), and other manipulators are used with arguments. In the header file, each of them is declared. Include this header file in your programme if you intend to utilise these manipulators. To specify the minimum width and fill the vacant area with any character you choose, for instance, use the manipulators below: Standard::cout, Standard::Setw, Standard::Setfill ('*');
Parameterized Manipulators:-
Manipulator -> Meaning
setw (int n) -> To set field width to n
setprecision (int p) -> The precision is fixed to p
setfill (Char f) -> To set the character to be filled
setiosflags (long l) -> Format flag is set to l
resetiosflags (long l) -> Removes the flags indicated by l
Setbase(int b) -> To set the base of the number to b
3. Manipulators in C++ contain the method setw ():
Whitespace is inserted between two variables via the output manipulator setw(). An integer number that is equal to the required space must be entered.
Syntax:
setw ( int n) As an example, int a=10; int b=20; cout << setw(10) << a << setw(10) << b << endl;
4. setfill() is a function in Manipulators in C++:
Using the C++ method setfill(), setw(whitespaces) is replaced with a different character. In that it modifies output, it's comparable to setw(), but the only input argument needed is a single character.
Syntax:
setfill(char Ch)
Example:
int a,b; a=10; b=20; cout<< setfill('*') << endl; cout << setw(5) << a << setw(5) << a << endl;
5. setprecision() is a function in Manipulators in C++:
A floating point integer's presentation of digits after the decimal point is controlled by an output manipulator.
Syntax:
setprecision (int p)
Example:
float A = 2.314; cout <<fixed<< setprecision(3) << A << endl;
6. setbase() is a function in Manipulators in C++:
The base of a number can be changed by using the setbase() manipulator. The C++ language is compatible with the following basic values:
- Hex (Hexadecimal value: 16)
- Oct (Octal: 8
- Dec (10 in decimal form).
Example:
#include <iostream> #include <iomanip> using namespace std; main() { int number = 100; cout << "Hex Value =" << " " << hex << number << endl; cout << "Octal Value=" << " " << oct << number << endl; cout << "Setbase Value=" << " " << setbase(8) << number << endl; cout << "Setbase Value=" << " " << setbase(16) << number << endl; return 0; }
Output
Hex Value = 64 Octal Value= 144 Setbase Value= 144 Setbase Value= 64
Language manipulators are used in C++ programming for output formatting. The input and output streams can be modified with the aid of these. To change the output, they employ the insertion and extraction operators.