Manipulators in C++
Introduction:
An object or function known as a manipulator in C++ is used with input and output streams (such as cin and cout) to change or regulate how data is formatted as it is read from or written to the stream. Typically, manipulators are used to alter how data is presented, such as changing the accuracy of floating-point values, adjusting output field widths, or showing numbers in various bases (such as decimal and hexadecimal).
The stream insertion () and extraction (>>) operators are frequently used to execute manipulators, and they are applied to the stream object itself. Both input streams (>>) and output streams () can be used with manipulators to change how data is read and shown, respectively.
Example:
#include <iostream>
#include <iomanip> // This header is required for manipulators
int main() {
double pi = 3.14159265359;
// Set precision for floating-point output
std::cout << "Default precision: " << pi << std::endl;
std::cout << "Precision set to 2 decimal places: " << std::fixed << std::setprecision(2) << pi << std::endl;
// Set field width for output
int number = 42;
std::cout << "Default field width: " << number << std::endl;
std::cout << "Field width set to 5: " << std::setw(5) << number << std::endl;
// Display a number in hexadecimal
int hexNumber = 255;
std::cout << "Default base: " << hexNumber << std::endl;
std::cout << "Display in hexadecimal: " << std::hex << hexNumber << std::endl;
return 0;
}
Output:
![Manipulators in C++](https://static.tutorialandexample.com/cpp/manipulators-in-cpp1.png)
Explanation:
- The accuracy of the pi variable is set to two decimal places using the std::fixed manipulator.
- The std::setw(5) manipulator changes the number variable's field width to 5 characters.
- The hexNumber variable is shown in hexadecimal representation using the std::hex manipulator.
Types of manipulators in C++:
- Sets the field's breadth to n characters with setw(int n). This is frequently used to determine the output field's minimum width. For instance, the output of the command cout setw(5) 42; is " 42" (with leading spaces to give it 5 characters wide).
- Sets the accuracy for floating-point values to n decimal places with the setprecision(int n) function. It impacts how many digits are shown after the decimal point. For instance, the result of the command cout fixed setprecision(2) 3.14159; is "3.14".
- Sets the display of floating-point numbers in defined-point notation (with a preset number of decimal places) when used with setprecision.
- When combined with setprecision, the term "scientific" indicates that numbers with floating points should be shown using the notation used in science.
- Bool alpha: When used with bool values, it renders them as "true" or "false" rather than the numeric values 1 or 0.
- Uppercase: It enables the letters to be shown in uppercase when paired with hexadecimal output. For instance, cout hex uppercase 255 will output "FF" rather than "ff."
- When used for integral types like hex or oct, showbase displays the base indicator in the output ("0x" for hexadecimal, "0" for octal).
- Sets the result of the justification to be aligned to the left. Output is right-aligned by default. For instance, the output of the command cout left setw(5) 42; is "42" with spaces to the right of it.
- Right: By default, the output justification is set to be right-aligned.
- noshowpoint: Prevents the floating-point number's decimal point and trailing zeroes from being displayed.
- noshowpos: Removes the plus sign from positive numbers when they are displayed.
- Boolean values are shown as "true" or "false" rather than 1 or 0.
- skipws: When receiving from an input stream, skips leading whitespace.
- When reading form an input stream, the noskipws option prevents bypassing of leading whitespace (the default setting).
Different methods of using manipulators:
- With the Insertion Operator (): The insertion () operator is frequently used in conjunction with manipulators to format output.
Example:
#include <iostream>
#include <iomanip>
int main() {
double pi = 3.14159265359;
std::cout << "Formatted Pi: " << std::fixed << std::setprecision(2) << pi << std::endl;
return 0;
}
Output:
![Manipulators in C++](https://static.tutorialandexample.com/cpp/manipulators-in-cpp2.png)
- The extraction (>>) operator arranges the input with extraction.
Example:
#include <iostream>
#include <iomanip>
int main() {
double value;
std::cout << "Enter a formatted floating-point number: ";
std::cin >> std::fixed >> std::setprecision(2) >> value;
std::cout << "You entered: " << value << std::endl;
return 0;
}
Output:
![Manipulators in C++](https://static.tutorialandexample.com/cpp/manipulators-in-cpp3.png)
- Manipulators can be linked together to provide more intricate formatting.
Example:
#include <iostream>
#include <iomanip>
int main() {
double pi = 3.14159265359;
std::cout << "Formatted Pi: " << std::setw(8) << std::fixed << std::setprecision(2) << pi << std::endl;
return 0;
}
Output:
![Manipulators in C++](https://static.tutorialandexample.com/cpp/manipulators-in-cpp4.png)
- Using Manipulators When Calling Functions: Manipulators can also be used when calling functions that use stream objects as parameters.
Example:
#include <iostream>
#include <iomanip>
void printFormattedValue(double value) {
std::cout << "Formatted Value: " << std::fixed << std::setprecision(2) << value << std::endl;
}
int main() {
double pi = 3.14159265359;
printFormattedValue(pi);
return 0;
}
Output:
![Manipulators in C++](https://static.tutorialandexample.com/cpp/manipulators-in-cpp5.png)
- Manipulators as Standalone Statements: To modify the formatting of a stream for upcoming input or output operations inside a certain scope, you can also utilize manipulators as standalone statements.
Example:
#include <iostream>
#include <iomanip>
int main() {
double pi = 3.14159265359;
std::cout << "Original Pi: " << pi << std::endl;
{
std::cout << "Formatted Pi: ";
std::cout << std::fixed << std::setprecision(2) << pi << std::endl;
}
std::cout << "Back to Original Pi: " << pi << std::endl;
return 0;
}
Output:
![Manipulators in C++](https://static.tutorialandexample.com/cpp/manipulators-in-cpp6.png)