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/ManipulatorsFeaturesExample CodeOutput 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::leftSet Left Alignmentstd::cout << std::left << std::setw(5) << 42;42
std::rightSet Right Alignmentstd::cout << std::right << std::setw(5) << 42;42
std::internalSign to the left, the rest to the rightstd::cout << std::internal << std::setw(5) << -42;- 42
std::setprecision(int)Set the number of significant digits for floating-point numbersstd::cout << std::setprecision(3) << 3.14159;3.14
std::fixedSet fixed-point notation to output floating-point numbersstd::cout << std::fixed << std::setprecision(2) << 3.14159;3.14
std::scientificSet scientific notation format to output floating-point numbersstd::cout << std::scientific << 3.14159;3.141590e+00
std::hexSet integers to display in hexadecimal (base 16)std::cout << std::hex << 42;2a
std::octSet integers to display in octal (base 8)std::cout << std::oct << 42;52
std::decSet integers to display in base 10 (default).std::cout << std::dec << 42;42
std::showbaseDisplay the base prefix (e.g.0xrepresenting hexadecimal)std::cout << std::showbase << std::hex << 42;0x2a
std::noshowbaseHide the base prefix (default)std::cout << std::noshowbase << std::hex << 42;2a
std::uppercaseDisplay hexadecimal letters in uppercasestd::cout << std::uppercase << std::hex << 42;2A
std::nouppercaseHexadecimal letters are displayed in lowercase (default).std::cout << std::nouppercase << std::hex << 42;2a
std::showposDisplay before positive numbers+symbolstd::cout << std::showpos << 42;+42
std::noshowposDo not display for positive numbers+Sign (default)std::cout << std::noshowpos << 42;42
std::boolalphaBoolean valuestrue/falseOutputstd::cout << std::boolalpha << true;true
std::noboolalphaBoolean 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::resetiosflagsReset the specified stream statestd::cout << std::resetiosflags(std::ios::showbase) << std::hex << 42;2a
std::setiosflagsSet the specified stream statestd::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 <iostream>
#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 <iostream>
#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 <iostream>
#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 <iostream>
#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 <iostream>
#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
ff
other extensions