好的!既然你提到了 C++ 标准库 <iomanip>,我将为你详细讲解它的用法,包括基本概念、主要功能和实用示例。<iomanip> 是 C++ 标准库中用于输入输出格式控制的头文件,通常与 <iostream> 配合使用,帮助你精确控制数据的显示方式(如宽度、对齐、精度等)。以下是完整的内容,适合学习和实践。


C++ 标准库:<iomanip>

<iomanip> 提供了格式化输入输出的工具,主要用于:

  • 控制输出格式:设置宽度、对齐方式、填充字符、数字精度等。
  • 增强可读性:让数据以更美观或标准化的方式显示。
  • 灵活性:适用于数字、浮点数、字符串等各种数据类型。

包含头文件

#include <iomanip>
#include <iostream> // 通常需要配合使用

常用操纵符(Manipulators)

<iomanip> 提供了一系列函数(称为操纵符),通过流操作符 <<>> 应用到输入输出流上。以下是主要功能:

  1. std::setw(n):设置字段宽度为 n 个字符(仅影响下一次输出)。
  2. std::setprecision(n):设置浮点数的显示精度为 n 位(包括整数部分和小数部分,默认是总位数)。
  3. std::fixed:以固定小数点格式显示浮点数(与 setprecision 配合,小数点后精度)。
  4. std::scientific:以科学计数法显示浮点数。
  5. std::left / std::right / std::internal:设置对齐方式(左对齐、右对齐、符号内部对齐)。
  6. std::setfill(c):设置填充字符为 c(与 setw 配合使用)。
  7. std::boolalpha / std::noboolalpha:布尔值显示为 “true”/”false” 或 1/0。

示例代码

1. 设置宽度和对齐 (setw, left, right)

#include <iomanip>
#include <iostream>

int main() {
    std::cout << std::setw(10) << std::left << "Name" << "Age" << std::endl;
    std::cout << std::setw(10) << std::left << "Alice" << "25" << std::endl;
    std::cout << std::setw(10) << std::right << "Bob" << "30" << std::endl;
    return 0;
}

输出:

Name      Age
Alice     25
     Bob  30
  • setw(10) 确保每个字段占 10 个字符。
  • leftright 控制对齐方向。

2. 设置浮点数精度 (setprecision, fixed)

#include <iomanip>
#include <iostream>

int main() {
    double pi = 3.1415926535;
    std::cout << "Default: " << pi << std::endl;           // 默认 6 位
    std::cout << std::setprecision(4) << "Precision 4: " << pi << std::endl; // 总位数 4
    std::cout << std::fixed << std::setprecision(4) << "Fixed 4: " << pi << std::endl; // 小数点后 4 位
    std::cout << std::scientific << "Scientific: " << pi << std::endl; // 科学计数法
    return 0;
}

输出:

Default: 3.14159
Precision 4: 3.142
Fixed 4: 3.1416
Scientific: 3.1416e+00
  • setprecision 单独使用时控制总有效数字。
  • fixed 后,setprecision 只控制小数点后位数。

3. 设置填充字符 (setfill)

#include <iomanip>
#include <iostream>

int main() {
    std::cout << std::setfill('*') << std::setw(10) << 42 << std::endl;
    std::cout << std::setfill('-') << std::setw(10) << "Hi" << std::endl;
    return 0;
}

输出:

*******42
-------Hi
  • setfill 指定空白处的填充字符,默认是空格。

4. 布尔值格式化 (boolalpha)

#include <iomanip>
#include <iostream>

int main() {
    bool flag = true;
    std::cout << "Default: " << flag << std::endl;          // 输出 1
    std::cout << std::boolalpha << "Boolalpha: " << flag << std::endl; // 输出 true
    std::cout << std::noboolalpha << "Noboolalpha: " << flag << std::endl; // 恢复为 1
    return 0;
}

输出:

Default: 1
Boolalpha: true
Noboolalpha: 1

5. 综合应用:表格输出

#include <iomanip>
#include <iostream>

int main() {
    std::cout << std::left << std::setfill('-');
    std::cout << std::setw(15) << "Name" << std::setw(10) << "Score" << std::endl;
    std::cout << std::setw(15) << "Alice" << std::fixed << std::setprecision(2) << std::setw(10) << 95.678 << std::endl;
    std::cout << std::setw(15) << "Bob" << std::setw(10) << 88.5 << std::endl;
    return 0;
}

输出:

Name-----------Score-----
Alice----------95.68-----
Bob------------88.50-----
  • 结合多种操纵符,制作整齐的表格。

注意事项

  1. 临时性setw 只影响下一次输出,其他操纵符(如 fixedsetfill)是持久的,直到被更改。
  2. 流状态:格式设置会保留在流对象中,需手动重置(如用 std::cout.unsetf(std::ios::fixed))。
  3. 精度限制:浮点数的实际精度受数据类型限制(如 double 约 15-17 位)。
  4. 输入支持<iomanip> 主要用于输出,输入格式控制较少。

小练习

以下是一个基于 <iomanip> 的练习题,你可以尝试:

问题:

写一个 C++ 程序,输出以下格式的表格:

Item      Price
----      -----
Apple     2.50
Banana    1.75
  • 要求:字段宽度为 10,左对齐,填充字符为 -,价格显示两位小数。

参考答案(你可以先尝试)

#include <iomanip>
#include <iostream>

int main() {
    std::cout << std::left << std::setfill('-') << std::fixed << std::setprecision(2);
    std::cout << std::setw(10) << "Item" << std::setw(10) << "Price" << std::endl;
    std::cout << std::setw(10) << "----" << std::setw(10) << "-----" << std::endl;
    std::cout << std::setw(10) << "Apple" << std::setw(10) << 2.5 << std::endl;
    std::cout << std::setw(10) << "Banana" << std::setw(10) << 1.75 << std::endl;
    return 0;
}

下一步

  • 如果你想试试这个练习,请写下你的代码,我会帮你检查。
  • 如果你想要更复杂的内容(比如格式化货币、时间,或结合其他库),或者设计 <iomanip> 的测验题,请告诉我!
  • 你也可以提出具体问题,比如“如何用 <iomanip> 输出十六进制?”或“怎么控制小数点前填充?”。

现在,你想做什么?试试练习,还是有其他需求?