In the C++ programming language, the term 'manipulators' means to manipulate or modify something. Manipulators are used as helping functions that can modify the I/O streams and can be included in the I/O statements to modify the format parameters of a stream. These manipulators are defined under two header files, which are <iostream> and <iomanip>. These manipulators use insertion ( << ) and extraction ( >> ) operators for modifying the output.
Categories of Manipulators in C++
We can classify manipulators into two categories:
- Non-Parameterized (Simple) Manipulators
- Parameterized Manipulators
Here, we will examine these modifiers individually.
Non-parametrized (Plain) Manipulators
In C++, non-parameterized manipulators refer to predefined stream manipulators that do not require any arguments. When incorporated in C++ code, these manipulators can directly impact the format and functionality of the input/output stream, primarily serving to regulate how output is presented. These manipulators are accessible through the <iomanip> header file. Non-parameterized manipulators encompass a variety of manipulators including endl, flush, ws, boolalpha, noboolalpha, dec, hex, oct, showbase, showpos, and more.
Parameterized Manipulators
In C++ development, the parameterized manipulators stand out as unique manipulators that can take arguments and are typically declared in the C++ standard <iomanip> header file. Unlike manipulators without parameters such as endl, setw, and hex, these manipulators need extra information to adjust the stream formatting.
A manipulator with parameters is a function that accepts one or more arguments and gives back a stream reference, enabling adjustments to its state or formatting characteristics. These manipulators are valuable for configuring precision, width, alignment, fill characters, and other settings.
Types of Manipulators
There are primarily five categories of manipulators in C++. These include:
Here, we will explore each of these manipulators individually.
Input Stream Manipulators
In C++, input stream manipulators are employed to alter the functionality of the input stream, aiding in the handling of input values. For instance, in cases where there are extraneous whitespaces present, utilizing ws allows us to bypass these whitespaces.
Although there are various input stream manipulators, a few common from them are:
- ws: It is available in the <iostream> header file. It is used to skip the leading white spaces in the input stream.
- noskipws: This input stream manipulator is available in the <iostream> header file. It is used to disable skipping of the leading whitespaces.
Input Stream Manipulator Example
Let's consider an example to demonstrate the Input Stream Manipulator in C++.
#include <iostream>
using namespace std; //using standard namespace
int main( ) { //main function
char c1, c2;
// Input skips whitespace by default
cin >> c1;
// Input the next character without skipping whitespace
cin >> noskipws >> c2;
cout << "c1: " << c1 << ", c2: " << c2;
return 0;
}
Input:
Output:
c1: x, c2: y
Explanation:
In this instance, we are working with two character variables referred to as c1 and c2. Following this, a user input prompt requests the values for c1 and c2 without any spaces. Subsequently, the cout function is employed to display the values of c1 and c2.
Output Stream Manipulators
In C++, output stream manipulators play a vital role in formatting and managing the output stream. They are beneficial when aiming for an improved display of the output. For instance, when there is a need to adjust the precision, width, or alignment of the output value, employing the output stream manipulator is recommended.
Although there are several output stream manipulators available, a few of them are as follows:
- Showpoint: It is used to display the decimal point forcefully, and it is available in the <iomanip> header file.
- Noshowpoint: It is used to hide the decimal point unless it is required, and it is present in the <iomanip> header file.
- Endl: It is used to insert a new line and flush the output stream, and it is available in the <iostream> header file.
- Fixed: It is used to display the numbers in fixed-point notation, and it is present in the < iomanip> header file.
- Flush: It is used to flush the output stream manually, and it is available in the <iostream> header file.
- setw(a): It is used to set the width of the next output field to a, and it is available in the <iomanip> header file.
- scientific: It is mainly used to display the numbers in scientific notation, and it is available in the <iomanip> header file.
- setprecision(a): It is used to set the precision for floating-point numbers to a, and it is available in the <iomanip> header file.
Output Stream Manipulators Example
Let's consider a scenario to demonstrate the utilization of output stream manipulators in the C++ programming language.
Example
#include <iostream>
#include <iomanip> // Required for manipulators like setw, setfill, setprecision
using namespace std; //using standard namespace
int main() { //main function
int a = 25;
double pi = 3.14159;
// Using endl
cout << "Using endl manipulator:" << endl;
cout << "Hello" << endl << "Cpp Tutorial" << endl;
// Using setw
cout << "\nUsing setw manipulator:" << endl;
cout << setw(6) << a << endl;
// Using setfill
cout << "\nUsing setfill with setw:" << endl;
cout << setfill('+') << setw(6) << a << endl;
// Using setprecision
cout << "\nUsing setprecision:" << endl;
cout << setprecision(3) << pi << endl;
cout << fixed << setprecision(4) << pi << endl;
return 0;
}
Output:
Hello
Cpp Tutorial
Using setw manipulator:
25
Using setfill with setw:
++++25
Using setprecision:
3.14
3.1416
Explanation:
In this instance, we showcase the application of C++ output stream manipulators like endl, setw, setfill, and setprecision. These manipulators are responsible for formatting the output through the management of line breaks, column width, filler characters, and decimal accuracy.
Base Manipulators
In C++ programming, base manipulators are primarily used to alter the numeral system in which integer values are displayed on the output stream. They are beneficial for formatting numbers in various bases like decimal, octal, and hexadecimal representations.
There are several common base manipulators in C++. Some of them are as follows:
- hex: It shows the output in hexadecimal base, and it is available in the <iostream> header file.
- dec: It shows the output in decimal base, and it is available in the <iostream> header file.
- oct: It shows the output in octal base, and it is available in the <iostream> header file.
Base Manipulators Example in C++
Let's consider a scenario to demonstrate the fundamental manipulator in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
int main() { //main function
int num = 58;
cout << "Decimal: " << dec << num << endl;
cout << "Octal: " << oct << num << endl;
cout << "Hexadecimal: " << hex << num << endl;
return 0;
}
Output:
Decimal: 58
Octal: 72
Hexadecimal: 3a
Explanation:
In this instance, we showcase the application of fundamental format modifiers to exhibit the integer num in various numerical systems. Subsequently, it showcases the value 58 in decimal, octal, and hexadecimal representations utilizing the cout stream.
Boolean Manipulators
In C++, the Boolean manipulators are primarily used to structure Boolean values within the output stream. These manipulators enable the presentation of Boolean values as either true or false, or as 0 or 1, depending on the specific system requirements. Nevertheless, we have the flexibility to adjust the output formatting by employing Boolean manipulators like std::boolalpha and std::noboolalpha.
There are mainly two types of Boolean manipulators in C++. These are as follows:
- boolalpha: It is used to display the true or false value for Boolean values, and is available in the <iostream> header file.
- noboolalpha: It is used to display the 0 or 1 value for Boolean values, and is available in the <iostream> header file
Boolean Manipulators Example in C++
Let's consider a scenario to demonstrate the Boolean manipulators in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
int main() { //main function
bool val = true;
// Default output
cout << "Default bool output: " << val << endl;
// Using boolalpha
cout << boolalpha << "Using boolalpha: " << val << endl;
// Using noboolalpha
cout << noboolalpha << "Using noboolalpha: " << val << endl;
return 0;
}
Output:
Default bool output: 1
Using boolalpha: true
Using noboolalpha: 1
Explanation:
In this instance, we showcase the Boolean manipulator in C++. Initially, we define a Boolean variable called val and set it to true. Subsequently, we employ the boolalpha and noboolalpha manipulators to format the Boolean value. Ultimately, the result is presented using the cout function.
Alignment and Sign Manipulators
In C++, alignment and sign modifiers are primarily employed for managing the alignment of text and numbers as well as determining how their signs should be presented in the output. These modifiers can be applied with output streams (cout) and are frequently combined with formatting functions in the <iomanip> header.
There are several alignment and sign manipulators in C++. Some of them are as follows:
- left: It is used to align the output to the left, and is used with the <iomanip> header file.
- right: It is used to align the output to the right, and is used with the <iomanip> header file.
- internal: It is used to align the signs and base prefixes to the left, and is used with the <iomanip> header file.
- showpos: It is used to show a plus (+) sign for positive numbers, and is used with the <iostream> header file.
- noshowpos: It is used to hide the plus (+) sign for positive numbers, and is used with the <iostream> header file.
Alignment and Sign Manipulators Example in C++
Let's consider a scenario to demonstrate the usage of alignment and sign manipulators in the C++ programming language.
Example
#include <iostream>
#include <iomanip> // using for setw, left, right, internal, showpos
using namespace std; //using standard namespace
int main() { //main function
int x = 27;
cout << "Default (right aligned):" << endl;
cout << setw(10) << x << endl; //default right aligned
cout << "\nLeft aligned:" << endl;
cout << left << setw(10) << x << endl; //for left aligned
cout << "\nInternal alignment with sign: " << endl;
cout << internal << showpos << setw(10) << x << endl; //for internal alignment with sign
cout << "\nRight aligned with sign: " << endl;
cout << right << showpos << setw(10) << x << endl; //for right aligned with sign
cout << "\nRight aligned with sign:" << endl;
cout << right << noshowpos << setw(10) << x << endl; //for right aligned with sign using npshowpos
return 0;
}
Output:
Default (right aligned):
27
Left aligned:
27
Internal alignment with sign:
+ 27
Right aligned with sign:
+27
Right aligned with sign:
27
Explanation:
In this instance, we showcase the utilization of alignment and sign modifiers for formatting integer results. Initially, setw(10) establishes the width of the output field as 10 characters, with left, right, and internal dictating the positioning of the integer within this field. Subsequently, showpos and noshowpos modifiers govern the display of the positive sign (+) for positive integers.
C++ Manipulators MCQs
1) Identify the C++ manipulator that we can use to set the decimal precision of floating-point numbers.
- std::setw
- std::setprecision
- std::fixed
- std::setfill
2) Identify the correct usage of the ‘std::fixed’ manipulator in C++.
- To set the width of the output field
- To set the output in to be in scientific notation
- To fill empty spaces with a specified character
- To set the output in to be in fixed-point notation
To format the output in fixed-point notation, choose option d).
3) Which C++ manipulator should we use to display the given input in hexadecimal format?
- std::oct
- std::fixed
- std::hex
- std::dec
4) Which C++ manipulator can we use for flushing the output buffer without the need to insert a newline?
- std::flush
- std::setfill
- std::endl
- std::setw
5) Which of the following is the correct use of the std::showpos manipulator in C++?
- It set the output to an octal format
- It shows a positive sign for positive numbers
- It sets the output to hexadecimal format.
- It hides the positive sign for positive numbers.
b) It indicates the positive value for numbers that are positive.