目录
1. 设置字段宽度:std::setw()
2. 设置浮点数精度:std::setprecision()
3. 设置填充字符:std::setfill()
4. 控制对齐方式:std::left 和 std::right,std::internal
5. 控制进制输出:std::hex、std::dec、std::oct
6. 显示进制前缀:std::showbase 、大写输出:std::uppercase
7. 控制布尔值输出格式:std::boolalpha
8. 显示正号+:std::showpos
9. 针对浮点型:std::fixed,std::scientific
10. 解除设置,恢复默认格式:std::resetiosflags()
11. 设置读取字符数:setw()
12. 保存与恢复流状态:std::ios::fmtflags
<iomanip> 是 C++ 标准库中的一个头文件,用于控制输入输出的格式。它提供了一些流操作符(manipulators),可以帮助你更方便地格式化输出,比如设置宽度、精度、填充字符等。
以下是 `<iomanip>` 中一些常用的操作符:
1. 设置字段宽度:std::setw()
- 用于设置输出的宽度。
#include <iostream>#include <iomanip>int main() {std::cout << std::setw(10) << 123 << std::endl; // 输出宽度为10,右对齐return 0;}
输出:
```
123
```
2. 设置浮点数精度:std::setprecision()
- 用于设置浮点数输出的精度(小数点后的位数)。
#include <iostream>#include <iomanip>int main() {double pi = 3.1415926535;std::cout << std::setprecision(4) << pi << std::endl; // 输出3.142return 0;}
3. 设置填充字符:std::setfill()
- 用于设置填充字符,通常与std::setw 一起使用。
#include <iostream>#include <iomanip>int main() {std::cout << std::setw(10) << std::setfill('*') << 123 << std::endl; // 输出*******123return 0;}
4. 控制对齐方式:std::left 和 std::right,std::internal
- std::left:左对齐。
- std::right:右对齐(默认)。
- std::internal:针对数值右对齐,如针对-123,可对齐为-****123。
#include <iostream>#include <iomanip>int main() {std::cout << std::left << std::setw(10) << 123 << std::endl; // 左对齐std::cout << std::right << std::setw(10) << 123 << std::endl; // 右对齐return 0;}
#include <iostream>#include <iomanip>int main() {int num = -123;std::cout << std::setw(8) << std::internal << std::setfill('*') << num << std::endl; // 输出 -***123return 0;}
5. 控制进制输出:std::hex、std::dec、std::oct
- std::hex:输出十六进制。
- std::dec:输出十进制(默认)。
- std::oct:输出八进制。
#include <iostream>#include <iomanip>int main() {int num = 255;std::cout << std::hex << num << std::endl; // 输出ffstd::cout << std::dec << num << std::endl; // 输出255std::cout << std::oct << num << std::endl; // 输出377return 0;}
6. 显示进制前缀:std::showbase 、大写输出:std::uppercase
#include <iostream>#include <iomanip>int main() {int num = 255;std::cout << std::showbase << std::uppercase << std::hex << num << std::endl; // 输出 0XFFreturn 0;}
7. 控制布尔值输出格式:std::boolalpha
- 将布尔值输出为 true/false,而不是 1/0。
#include <iostream>int main() {bool flag = true;std::cout << std::boolalpha << flag << std::endl; // 输出truereturn 0;}
8. 显示正号+:std::showpos
- showpos 强制正数输出时显示正号。
代码示例如8图.
9. 针对浮点型:std::fixed,std::scientific
- fixed 针对浮点型变量以固定的小数位数进行输出的设置。(又称定点表示法)
- scientific 针对浮点型变量以科学计数法的形式输出。
#include <iostream>#include <iomanip>int main() {double num = 123.456;// 强制显示正号,定点表示法,保留两位小数std::cout << std::showpos << std::fixed << std::setprecision(2) << num << std::endl; // 输出 +123.46return 0;}
#include <iostream>#include <iomanip>int main() {double num = 123.456789;std::cout << std::fixed << std::setprecision(3) << num << std::endl; // 123.457std::cout << std::scientific << std::setprecision(3) << num << std::endl; // 1.235e+02return 0;}
科学计数法:编程中的科学计数法
另外,有fixed参与的设置(如上的 std::showpos << std::fixed << std::setprecision(2))还会作用于以后的所有代码,所以需要搭配 std::resetiosflags(std::ios::fixed) 来解除。
10. 解除设置,恢复默认格式:std::resetiosflags()
- 恢复默认格式。
#include <iostream>#include <cstdio>#include <iomanip>using namespace std;int main(){int n = 7;double m = 3.1415926; //重点关注 m 的输出变化cout << fixed << setprecision(2) << n << endl; //7cout << m << endl; //3.14cout << n << endl; //7cout << resetiosflags(ios::fixed) << n << endl; //7cout << m << endl; //3.1415926}
11. 设置读取字符数:setw()
- 限制读取的字符数。
#include <iostream>#include <iomanip>int main() {char buffer[10];std::cout << "输入一个单词(最多4字符):";std::cin >> std::setw(5) >> buffer; // 防止缓冲区溢出std::cout << "读取的内容:" << buffer << std::endl;return 0;}
12. 保存与恢复流状态:std::ios::fmtflags
- 这是个数据类型,用于修改流后,对这修改方案的保存。
#include <iostream>#include <iomanip>int main() {std::ios::fmtflags original_flags = std::cout.flags(); // 保存原始状态std::cout << std::hex << std::showbase << 255 << std::endl; // 输出 0xffstd::cout.flags(original_flags); // 恢复状态std::cout << 255 << std::endl; // 输出 255(十进制)return 0;}
其成员函数有三,
- flags,用于获取当前的输出流格式,
std::ios::fmtflags flags = std::cout.flags();
- setf(...),用于设定格式标志,如hex、showbase等,
std::cout.setf(std::ios_base::hex, std::ios_base::basefield); std::cout.setf(std::ios_base::showbase);
- unsetf(),用于清除指定格式标志,重置为默认值,
#include <iostream>int main() {// 设置格式标志std::cout.setf(std::ios::hex, std::ios::basefield);std::cout.setf(std::ios::showbase);// 输出一个整数std::cout << 42 << std::endl;// 关闭设置的格式标志std::cout.unsetf(std::ios::hex);std::cout.unsetf(std::ios::showbase);return 0;}