Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

The C++ standard library

The C++ standard library

is a header file in the C++ standard library that provides formatting operations for input/output streams.

iomanip The functions in the library allow developers to control output format, such as setting the number of decimal places, setting width, and alignment.

iomanip is short for Input/Output Manipulators; it provides a set of manipulators for controlling the format of I/O streams in the C++ standard library, applicable to the following scenarios:

Syntax

iomanip The functions in the library are usually used together with << and >> operator to control the output stream.

Here are some commonly used iomanip functions as follows:

functions/manipulators Function Example code Output result
std::setw(int n) Sets the field width, specifying the width for the next output std::cout << std::setw(5) << 42; 42
std::setfill(char) Sets the 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, rest right std::cout << std::internal << std::setw(5) << -42; - 42
std::setprecision(int) Sets the significant digits of floating-point numbers std::cout << std::setprecision(3) << 3.14159; 3.14
std::fixed Sets fixed-format output for floating-point numbers std::cout << std::fixed << std::setprecision(2) << 3.14159; 3.14
std::scientific Sets scientific-notation output for floating-point numbers std::cout << std::scientific << 3.14159; 3.141590e+00
std::hex Displays integers in base 16 (hexadecimal) std::cout << std::hex << 42; 2a
std::oct Displays integers in base 8 (octal) std::cout << std::oct << 42; 52
std::dec Displays integers in base 10 (decimal, default) std::cout << std::dec << 42; 42
std::showbase Shows the base prefix (e.g., 0x represents hexadecimal) std::cout << std::showbase << std::hex << 42; 0x2a
std::noshowbase Hides the 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 Shows a + sign before positive numbers std::cout << std::showpos << 42; +42
std::noshowpos Does not show the + sign for positive numbers (default) std::cout << std::noshowpos << 42; 42
std::boolalpha Booleans output as true/false Output std::cout << std::boolalpha << true; true
std::noboolalpha Booleans output as 1/0 (default) std::cout << std::noboolalpha << true; 1
std::setbase(int n) Sets the base of integers (supports 8, 10, 16) std::cout << std::setbase(16) << 42; 2a
std::resetiosflags Resets the specified stream state std::cout << std::resetiosflags(std::ios::showbase) << std::hex << 42; 2a
std::setiosflags Sets the specified stream state std::cout << std::setiosflags(std::ios::showbase) << std::hex << 42; 0x2a

Example

1. Setting width

Use setw can set the output width. If the output content has fewer characters than the set width, the remainder is padded with spaces.

Example

#include

#include

int

main

(

)

{

    std

::

cout

<<

std

::

setw

(

10

)

<<

"Hello"

<<

std

::

endl

;

   

return

0

;

}

Output result:

Hello

2. Setting precision

Use setprecision can set the significant digits of 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.14159

Set precision (3): 3.14

Set precision (7): 3.141593

3. Fixed decimal 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.000000

Scientific: 1.23456789e+08

4. Setting the fill character

Use 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 the stream's 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