C++ Standard Library <iomanip>
<iomanip>It is a header file in the C++ standard library that provides formatted operations for input/output streams.
iomanipThe functions in the library allow developers to control output formatting, such as setting the number of digits after the decimal point, setting width, alignment, and so on.
iomanipIt is the abbreviation for Input/Output Manipulators, which provides a set of operators for controlling the formatting of input/output streams in the C++ standard library, applicable to the following scenarios:
- Handling of floating-point formats in scientific computing;
- Data alignment and beautification;
- Display numeric values in a specific base or format.
Syntax
iomanipFunctions in the library are usually used with<<and>>Used together with operators to achieve control of the output stream.
The following are some commonly usediomanipFunction:
| Functions/Manipulators | Features | Example Code | Output result |
|---|---|---|---|
std::setw(int n) | Set field width, specifying the width for the next output. | std::cout << std::setw(5) << 42; | 42 |
std::setfill(char) | Set the fill character (default is space) | std::cout << std::setfill('*') << std::setw(5) << 42; | ***42 |
std::left | Set Left Alignment | std::cout << std::left << std::setw(5) << 42; | 42 |
std::right | Set Right Alignment | std::cout << std::right << std::setw(5) << 42; | 42 |
std::internal | Sign to the left, the rest to the right | std::cout << std::internal << std::setw(5) << -42; | - 42 |
std::setprecision(int) | Set the number of significant digits for floating-point numbers | std::cout << std::setprecision(3) << 3.14159; | 3.14 |
std::fixed | Set fixed-point notation to output floating-point numbers | std::cout << std::fixed << std::setprecision(2) << 3.14159; | 3.14 |
std::scientific | Set scientific notation format to output floating-point numbers | std::cout << std::scientific << 3.14159; | 3.141590e+00 |
std::hex | Set integers to display in hexadecimal (base 16) | std::cout << std::hex << 42; | 2a |
std::oct | Set integers to display in octal (base 8) | std::cout << std::oct << 42; | 52 |
std::dec | Set integers to display in base 10 (default). | std::cout << std::dec << 42; | 42 |
std::showbase | Display the base prefix (e.g.0xrepresenting hexadecimal) | std::cout << std::showbase << std::hex << 42; | 0x2a |
std::noshowbase | Hide the base prefix (default) | std::cout << std::noshowbase << std::hex << 42; | 2a |
std::uppercase | Display hexadecimal letters in uppercase | std::cout << std::uppercase << std::hex << 42; | 2A |
std::nouppercase | Hexadecimal letters are displayed in lowercase (default). | std::cout << std::nouppercase << std::hex << 42; | 2a |
std::showpos | Display before positive numbers+symbol | std::cout << std::showpos << 42; | +42 |
std::noshowpos | Do not display for positive numbers+Sign (default) | std::cout << std::noshowpos << 42; | 42 |
std::boolalpha | Boolean valuestrue/falseOutput | std::cout << std::boolalpha << true; | true |
std::noboolalpha | Boolean values1/0Output (default) | std::cout << std::noboolalpha << true; | 1 |
std::setbase(int n) | Set the base of integers (supports 8, 10, 16). | std::cout << std::setbase(16) << 42; | 2a |
std::resetiosflags | Reset the specified stream state | std::cout << std::resetiosflags(std::ios::showbase) << std::hex << 42; | 2a |
std::setiosflags | Set the specified stream state | std::cout << std::setiosflags(std::ios::showbase) << std::hex << 42; | 0x2a |
Example
1. Set width
UsagesetwCan set the width of the output. If the number of characters in the output content is less than the set width, the remaining part will be filled with spaces.
Example
#include <iomanip>
int main() {
std::cout << std::setw(10) << "Hello" << std::endl;
return 0;
}
Output:
Hello
2. Set precision
UsagesetprecisionCan set the number of significant digits for floating-point numbers.
Example
#include <iomanip>
int main() {
double pi = 3.141592653589793;
std::cout << "Default: " << pi << "\n";
std::cout << "Set precision (3): " << std::setprecision(3) << pi << "\n";
std::cout << "Set precision (7): " << std::setprecision(7) << pi << "\n";
return 0;
}
Output:
Default: 3.14159 Set precision (3): 3.14 Set precision (7): 3.141593
3. Fixed-point and scientific notation
fixedandscientificCan control the output format of floating-point numbers.
Example
#include <iomanip>
int main() {
double num = 123456789.0;
std::cout << "Fixed: " << std::fixed << num << std::endl;
std::cout << "Scientific: " << std::scientific << num << std::endl;
return 0;
}
Output:
Fixed: 123456789.000000 Scientific: 1.23456789e+08
4. Setting the fill character
UsagesetfillCan set the fill character, usually withsetwUsed together.
Example
#include <iomanip>
int main() {
std::cout << std::setfill('*') << std::setw(10) << "World" << std::endl;
return 0;
}
Output:
*****World
5. Setting and resetting format flags
setiosflagsandresetiosflagsCan set or reset the stream's format flags.
Example
#include <iomanip>
int main() {
std::cout << std::setiosflags(std::ios::uppercase) << std::hex << 255 << std::endl;
std::cout << std::resetiosflags(std::ios::uppercase) << std::hex << 255 << std::endl;
return 0;
}
Output:
FF ffother extensions