Cpp Libs Iomanip
`` is a header file in the C++ standard library that provides formatting operations for input/output streams.
The functions in the `iomanip` library allow developers to control output formats, such as setting the number of digits after the decimal point, setting width, alignment, etc.
`iomanip` is short for Input/Output Manipulators. It provides a set of manipulators for controlling the format of input/output streams in the C++ standard library, applicable to the following scenarios:
* Handling floating-point formats in scientific computing;
* Data alignment and beautification;
* Displaying values in specific bases or formats.
### Syntax
Functions in the `iomanip` library are typically used with the `<<` and `>>` operators to control output streams.
Here are some commonly used `iomanip` functions:
| **Function/Manipulator** | **Functionality** | **Example Code** | **Output Result** |
| --- | --- | --- | --- |
| `std::setw(int n)` | Sets field width, specifying width for the next output | `std::cout << std::setw(5) << 42;` | `42` |
| `std::setfill(char)` | Sets fill character (default is space) | `std::cout << std::setfill('*') << std::setw(5) << 42;` | `***42` |
| `std::left` | Sets left alignment | `std::cout << std::left << std::setw(5) << 42;` | `42` |
| `std::right` | Sets right alignment | `std::cout << std::right << std::setw(5) << 42;` | `42` |
| `std::internal` | Sign left-aligned, rest right-aligned | `std::cout << std::internal << std::setw(5) << -42;` | `- 42` |
| `std::setprecision(int)` | Sets significant digits for floating-point numbers | `std::cout << std::setprecision(3) << 3.14159;` | `3.14` |
| `std::fixed` | Sets fixed-point format for floating-point output | `std::cout << std::fixed << std::setprecision(2) << 3.14159;` | `3.14` |
| `std::scientific` | Sets scientific notation format for floating-point output | `std::cout << std::scientific << 3.14159;` | `3.141590e+00` |
| `std::hex` | Sets integers to display in hexadecimal | `std::cout << std::hex << 42;` | `2a` |
| `std::oct` | Sets integers to display in octal | `std::cout << std::oct << 42;` | `52` |
| `std::dec` | Sets integers to display in decimal (default) | `std::cout << std::dec << 42;` | `42` |
| `std::showbase` | Shows base prefix (e.g., `0x` for hexadecimal) | `std::cout << std::showbase << std::hex << 42;` | `0x2a` |
| `std::noshowbase` | Hides base prefix (default) | `std::cout << std::noshowbase << std::hex << 42;` | `2a` |
| `std::uppercase` | Displays hexadecimal letters in uppercase | `std::cout << std::uppercase << std::hex << 42;` | `2A` |
| `std::nouppercase` | Displays hexadecimal letters in lowercase (default) | `std::cout << std::nouppercase << std::hex << 42;` | `2a` |
| `std::showpos` | Displays `+` sign before positive numbers | `std::cout << std::showpos << 42;` | `+42` |
| `std::noshowpos` | Does not display `+` sign for positive numbers (default) | `std::cout << std::noshowpos << 42;` | `42` |
| `std::boolalpha` | Outputs boolean values as `true/false` | `std::cout << std::boolalpha << true;` | `true` |
| `std::noboolalpha` | Outputs boolean values as `1/0` (default) | `std::cout << std::noboolalpha << true;` | `1` |
| `std::setbase(int n)` | Sets the base for integers (supports 8, 10, 16) | `std::cout << std::setbase(16) << 42;` | `2a` |
| `std::resetiosflags` | Resets specified stream flags | `std::cout << std::resetiosflags(std::ios::showbase) << std::hex << 42;` | `2a` |
| `std::setiosflags` | Sets specified stream flags | `std::cout << std::setiosflags(std::ios::showbase) << std::hex << 42;` | `0x2a` |
## Examples
### 1. Setting Width
Using `setw` can set the output width. 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
#include
int main(){
std::cout<< std::setw(10)<<"Hello"<< std::endl;
return 0;
}
**Output Result:**
Hello
### 2. Setting Precision
Using `setprecision` can set the number of significant digits for floating-point numbers.
## Example
#include
#include
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 Result:**
Default: 3.14159Set precision (3): 3.14Set precision (7): 3.141593
### 3. Fixed Decimal Point and Scientific Notation
`fixed` and `scientific` can control the output format of floating-point numbers.
## Example
#include
#include
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 Result:**
Fixed: 123456789.000000Scientific: 1.23456789e+08
### 4. Setting Fill Character
Using `setfill` can set the fill character, usually used together with `setw`.
## Example
#include
#include
int main(){
std::cout<< std::setfill('*')<< std::setw(10)<<"World"<< std::endl;
return 0;
}
**Output Result:**
***** World
### 5. Setting and Resetting Format Flags
`setiosflags` and `resetiosflags` can set or reset stream format flags.
## Example
#include
#include
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 Result:**
FF ff
YouTip