<iomanip> 是 C++ 标准库中的一个头文件,它提供了对输入/输出流的格式化操作。
iomanip 库中的函数允许开发者控制输出格式,如设置小数点后的位数、设置宽度、对齐方式等。
iomanip 是 Input/Output Manipulators 的缩写,它提供了一组操作符,用于控制 C++ 标准库中的输入/输出流的格式,适用以下场景:
语法
iomanip 库中的函数通常与 << 和 >> 操作符一起使用,以实现对输出流的控制。
以下是一些常用的 iomanip 函数:
std::setw(int n)
std::cout << std::setw(5) << 42;
42
std::setfill(char)
std::cout << std::setfill('*') << std::setw(5) << 42;
***42
std::left
std::cout << std::left << std::setw(5) << 42;
std::right
std::cout << std::right << std::setw(5) << 42;
std::internal
std::cout << std::internal << std::setw(5) << -42;
- 42
std::setprecision(int)
std::cout << std::setprecision(3) << 3.14159;
3.14
std::fixed
std::cout << std::fixed << std::setprecision(2) << 3.14159;
std::scientific
std::cout << std::scientific << 3.14159;
3.141590e+00
std::hex
std::cout << std::hex << 42;
2a
std::oct
std::cout << std::oct << 42;
52
std::dec
std::cout << std::dec << 42;
std::showbase
0x
std::cout << std::showbase << std::hex << 42;
0x2a
std::noshowbase
std::cout << std::noshowbase << std::hex << 42;
std::uppercase
std::cout << std::uppercase << std::hex << 42;
2A
std::nouppercase
std::cout << std::nouppercase << std::hex << 42;
std::showpos
+
std::cout << std::showpos << 42;
+42
std::noshowpos
std::cout << std::noshowpos << 42;
std::boolalpha
true/false
std::cout << std::boolalpha << true;
true
std::noboolalpha
1/0
std::cout << std::noboolalpha << true;
1
std::setbase(int n)
std::cout << std::setbase(16) << 42;
std::resetiosflags
std::cout << std::resetiosflags(std::ios::showbase) << std::hex << 42;
std::setiosflags
std::cout << std::setiosflags(std::ios::showbase) << std::hex << 42;
实例
1. 设置宽度
使用 setw 可以设置输出的宽度。如果输出内容的字符数少于设置的宽度,剩余部分将用空格填充。
#include <iostream> #include <iomanip> int main() { std::cout << std::setw(10) << "Hello" << std::endl; return 0; }
输出结果:
Hello
2. 设置精度
使用setprecision 可以设置设置浮点数的有效位数。
#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; }
Default: 3.14159 Set precision (3): 3.14 Set precision (7): 3.141593
3. 固定小数点和科学计数法
fixed 和 scientific 可以控制浮点数的输出格式。
#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; }
Fixed: 123456789.000000 Scientific: 1.23456789e+08
4. 设置填充字符
使用 setfill 可以设置填充字符,通常与 setw 一起使用。
#include <iostream> #include <iomanip> int main() { std::cout << std::setfill('*') << std::setw(10) << "World" << std::endl; return 0; }
*****World
5. 设置和重置格式标志
setiosflags 和 resetiosflags 可以设置或重置流的格式标志。
#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; }
FF ff