跳转到内容
新建笔记

C++ 标准库头文件:容器、输入输出与线程

这里整理的是 C++ 标准库头文件,不是第三方库清单。一个头文件提供相关声明;使用什么设施,就直接包含相应头文件,不依赖其他头文件偶然替你包含它。下面以 C++17 为基准。标准头文件。

头文件用途与示例阅读时的边界
<iostream>std::cout << "Hello, World!" << '\n';std::endl 还会刷新流,普通换行不一定需要刷新
<vector>std::vector<int> v{1, 2, 3};连续存储的动态序列;扩容可能使原指针、引用和迭代器失效
<string>std::string text = "Hello";字节字符串,不自动等于按 Unicode 字符计数
<algorithm>std::sort(v.begin(), v.end());算法有迭代器和比较关系要求;不能用这个调用直接排序 list
<map>std::map<int, std::string> m{{1, "one"}, {2, "two"}};按比较器排列键;operator[] 可能插入新元素
<unordered_map>std::unordered_map<int, std::string> m{{1, "one"}};哈希表;迭代顺序不应作为输出协议
<set>std::set<int> s{1, 2, 3};按比较器组织不重复键,不能直接改元素破坏次序
<unordered_set>std::unordered_set<int> s{1, 2, 3};基于哈希和相等关系判断键,顺序不固定
<list>std::list<int> values{1, 2, 3};双向链表;自己的 sort() 可排序,不提供数组式随机访问
<stack>std::stack<int> s; s.push(1);后进先出的容器适配器;先检查非空再访问 top()
<queue>std::queue<int> q; q.push(1);先进先出的容器适配器;先检查非空再访问 front()
<fstream>std::ofstream out("example.txt");需要检查打开和写入结果;普通输出打开可能截断旧文件
<ctime>std::time_t now = std::time(nullptr);失败返回 time_t(-1);具体表示和精度由实现决定
<iomanip>std::fixed << std::setprecision(2)改变流格式状态;对后续输出仍有效
<thread>std::thread worker(task); worker.join();线程对象销毁前需处理 joinable 状态;共享数据仍需同步
<cmath>std::sin(1.0)三角函数使用弧度;定义域、取整与余数见数学函数
<cstddef>std::size_t count = 10;对象大小相关的无符号类型;不固定为 32 位或 64 位
<bitset>std::bitset<8> bits(42);固定 N 位的位序列;bits[0] 是最低位

可独立运行的组合示例

跳转到“可独立运行的组合示例”

这段程序实际使用上述 18 个头文件。文件名通过参数传入,便于在临时目录验证;运行时会写入该文件,请选择专门用于示例的路径。线程只在工作期间修改自己的结果,主线程在 join() 之后读取。

standard-headers.cpp
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <vector>
int main(int argc, char** argv) {
if (argc != 2) {
std::cerr << "usage: standard-headers <example-output-file>\n";
return 2;
}
std::cout << "Hello, World!\n";
std::vector<int> values{3, 1, 2};
std::sort(values.begin(), values.end());
assert(values == std::vector<int>({1, 2, 3}));
std::string text = "Hello";
std::map<int, std::string> ordered{{1, "one"}, {2, "two"}};
std::unordered_map<int, std::string> hashed{{1, "one"}, {2, "two"}};
assert(ordered.at(1) == "one" && hashed.at(2) == "two");
std::set<int> uniqueOrdered{1, 2, 3, 2};
std::unordered_set<int> uniqueHashed{1, 2, 3, 2};
assert(uniqueOrdered.size() == 3 && uniqueHashed.count(2) == 1);
std::list<int> linked{3, 1, 2};
linked.sort();
assert(linked.front() == 1 && linked.back() == 3);
std::stack<int> lastInFirstOut;
lastInFirstOut.push(1);
lastInFirstOut.push(2);
assert(!lastInFirstOut.empty() && lastInFirstOut.top() == 2);
lastInFirstOut.pop();
std::queue<int> firstInFirstOut;
firstInFirstOut.push(1);
firstInFirstOut.push(2);
assert(!firstInFirstOut.empty() && firstInFirstOut.front() == 1);
firstInFirstOut.pop();
{
std::ofstream output(argv[1]);
if (!output) { std::cerr << "cannot open output\n"; return 1; }
output << text;
output.close();
if (!output) { std::cerr << "cannot finish output\n"; return 1; }
}
std::ifstream input(argv[1]);
std::string loaded;
if (!(input >> loaded)) { std::cerr << "cannot read output\n"; return 1; }
assert(loaded == text);
const std::time_t now = std::time(nullptr);
std::ostringstream formatted;
formatted << std::fixed << std::setprecision(2) << 3.14159;
assert(formatted.str() == "3.14");
int workerResult = 0;
std::thread worker([&] { workerResult = 42; });
worker.join();
assert(workerResult == 42);
const double sine = std::sin(1.0);
assert(std::abs(sine - 0.8414709848078965) < 1e-12);
const std::size_t count = values.size();
const std::bitset<8> bits(42);
assert(count == 3 && bits.to_string() == "00101010" && !bits[0]);
std::cout << "headers OK; number=" << formatted.str()
<< "; time available=" << (now != std::time_t(-1)) << '\n';
}

<sstream> 和 <cassert> 是为了验证和展示结果而额外包含的头文件。GCC 编译示例为 g++ -std=c++17 -pthread standard-headers.cpp -o standard-headers,随后传入测试文件路径。这里的 join 建立完成等待;给任意共享变量加一个线程并不能自动保证并发安全。线程完成与等待。

第三方线性代数库另见 Eigen。选择库时应先分清“标准库已有设施”和“需要额外安装、配置包含路径的依赖”。