Skip to content

Commit 4463464

Browse files
Referenced Card Finished for m29 (#18)
Reference Card Finished
1 parent 6c176df commit 4463464

44 files changed

Lines changed: 3351 additions & 200 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: "std::jthread"
3+
description: "自动 join 的线程类,析构时发送停止请求并等待线程退出"
4+
chapter: 99
5+
order: 4
6+
tags:
7+
- host
8+
- cpp-modern
9+
- beginner
10+
difficulty: beginner
11+
cpp_standard: [20, 23]
12+
---
13+
14+
<!--
15+
参考卡模板 (Reference Card Template)
16+
用于 documents/cpp-reference/ 下的特性速查页。
17+
与 article-template.md 不同,参考卡走精炼的结构化格式,不需要叙事风格。
18+
19+
标签使用规则:
20+
1. 必须包含 1 个 platform 标签(参考卡统一用 host)
21+
2. 必须包含 1 个 difficulty 标签
22+
3. 至少包含 1 个 topic 标签
23+
4. 从 scripts/validate_frontmatter.py 的 VALID_TAGS 集合中选取
24+
-->
25+
26+
# std::jthread(C++20)
27+
28+
## 一句话
29+
30+
自带 RAII 语义的线程类——析构时自动发送停止请求并 join,彻底消灭忘记 join 导致的 crash。
31+
32+
## 头文件
33+
34+
`#include <thread>`
35+
36+
## 核心 API 速查
37+
38+
| 操作 | 签名 | 说明 |
39+
|------|------|------|
40+
| 构造(带函数) | `template<class F> jthread(F&& f, Args&&... args)` | 启动新线程执行 f(args...) |
41+
| 构造(带 stop_token) | `template<class F> jthread(F&& f)` | f 的首参接收 `std::stop_token` |
42+
| 析构 | `~jthread()` | 请求停止 + join(若 joinable) |
43+
| 请求停止 | `bool request_stop() noexcept` | 请求协作式停止,返回是否成功 |
44+
| 获取停止令牌 | `std::stop_token get_stop_token() const noexcept` | 获取当前线程的停止令牌 |
45+
| 等待完成 | `void join()` | 阻塞等待线程结束 |
46+
| 分离线程 | `void detach()` | 分离,线程独立运行 |
47+
| 是否可 join | `bool joinable() const noexcept` | 检查线程是否可 join |
48+
| 获取 ID | `std::thread::id get_id() const noexcept` | 返回线程标识 |
49+
50+
## 最小示例
51+
52+
```cpp
53+
// Standard: C++20
54+
#include <iostream>
55+
#include <thread>
56+
57+
void worker(std::stop_token st) {
58+
while (!st.stop_requested()) {
59+
std::cout << "working...\n";
60+
}
61+
std::cout << "stopped\n";
62+
}
63+
64+
int main() {
65+
std::jthread t(worker); // 自动传入 stop_token
66+
// t 析构时自动 request_stop() + join()
67+
} // 输出: working... stopped
68+
```
69+
70+
## 嵌入式适用性:中
71+
72+
- RAII 自动 join 消除忘记 join 的隐患,提升代码健壮性
73+
- `std::stop_token` 协作式取消机制比手工标志变量更规范
74+
- 依赖操作系统线程支持,裸机 RTOS 场景需配合线程抽象层使用
75+
- 需要 C++20 标准库支持,GCC 10+ 即可用,但 Clang/libc++ 支持较晚(17+)
76+
77+
## 编译器支持
78+
79+
| GCC | Clang | MSVC |
80+
|-----|-------|------|
81+
| 10 | 17 | 19.28 |
82+
83+
## 另见
84+
85+
- [cppreference: std::jthread](https://en.cppreference.com/w/cpp/thread/jthread)
86+
- [cppreference: std::stop_token](https://en.cppreference.com/w/cpp/thread/stop_token)
87+
88+
---
89+
90+
*部分内容参考自 [cppreference.com](https://en.cppreference.com/),采用 [CC-BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) 许可*

documents/cpp-reference/concurrency/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ tags:
1515
<ChapterLink href="01-atomic">std::atomic</ChapterLink>
1616
<ChapterLink href="02-thread">std::thread</ChapterLink>
1717
<ChapterLink href="03-mutex">std::mutex</ChapterLink>
18+
<ChapterLink href="04-jthread">std::jthread</ChapterLink>
1819
</ChapterNav>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: "std::filesystem"
3+
description: "跨平台的文件系统操作库:路径操作、目录遍历、文件状态查询"
4+
chapter: 99
5+
order: 6
6+
tags:
7+
- host
8+
- cpp-modern
9+
- beginner
10+
difficulty: beginner
11+
cpp_standard: [17, 20, 23]
12+
---
13+
14+
<!--
15+
参考卡模板 (Reference Card Template)
16+
用于 documents/cpp-reference/ 下的特性速查页。
17+
与 article-template.md 不同,参考卡走精炼的结构化格式,不需要叙事风格。
18+
19+
标签使用规则:
20+
1. 必须包含 1 个 platform 标签(参考卡统一用 host)
21+
2. 必须包含 1 个 difficulty 标签
22+
3. 至少包含 1 个 topic 标签
23+
4. 从 scripts/validate_frontmatter.py 的 VALID_TAGS 集合中选取
24+
-->
25+
26+
# std::filesystem(C++17)
27+
28+
## 一句话
29+
30+
平台无关的文件系统操作库:路径拼接与规范化、目录创建与遍历、文件复制与删除、权限与状态查询——告别 `stat()``opendir()`
31+
32+
## 头文件
33+
34+
`#include <filesystem>`
35+
36+
## 核心 API 速查
37+
38+
| 操作 | 签名 | 说明 |
39+
|------|------|------|
40+
| 路径类 | `class path` | 路径的构造、拼接、分解(跨平台分隔符处理) |
41+
| 路径拼接 | `path operator/(const path& lhs, const path& rhs)` | `p / "subdir" / "file.txt"` |
42+
| 当前路径 | `path current_path()` | 获取/设置工作目录 |
43+
| 目录迭代 | `class directory_iterator` | 遍历单层目录 |
44+
| 递归迭代 | `class recursive_directory_iterator` | 递归遍历子目录 |
45+
| 文件状态 | `bool exists(const path& p)` | 检查路径是否存在 |
46+
| 文件大小 | `uintmax_t file_size(const path& p)` | 获取文件字节数 |
47+
| 创建目录 | `bool create_directory(const path& p)` | 创建单个目录 |
48+
| 创建多级目录 | `bool create_directories(const path& p)` | 递归创建整个路径 |
49+
| 复制文件 | `bool copy_file(const path& from, const path& to)` | 复制单个文件 |
50+
| 删除 | `bool remove(const path& p)` | 删除文件或空目录 |
51+
| 递归删除 | `uintmax_t remove_all(const path& p)` | 递归删除目录及内容 |
52+
| 重命名 | `void rename(const path& old, const path& newp)` | 重命名或移动 |
53+
54+
## 最小示例
55+
56+
```cpp
57+
// Standard: C++17
58+
#include <filesystem>
59+
#include <iostream>
60+
61+
namespace fs = std::filesystem;
62+
63+
int main() {
64+
fs::path p = fs::current_path() / "test.txt";
65+
std::cout << p << "\n"; // 完整路径
66+
std::cout << p.filename() << "\n"; // test.txt
67+
std::cout << p.extension() << "\n"; // .txt
68+
69+
fs::create_directories("a/b/c"); // 递归创建
70+
std::cout << fs::exists("a/b") << "\n"; // true
71+
fs::remove_all("a"); // 递归删除
72+
}
73+
```
74+
75+
## 嵌入式适用性:低
76+
77+
- 依赖操作系统文件系统抽象层(POSIX 或 Win32),裸机环境无文件系统
78+
- 适用于嵌入式 Linux(如 Buildroot/Yocto 平台)或上位机配置/日志工具
79+
- 头文件引入的开销较大,资源极度受限的设备不建议使用
80+
- 对需要文件系统的嵌入式场景(如 FAT32 on SD card),可选用轻量级替代(如 LittleFS)
81+
82+
## 编译器支持
83+
84+
| GCC | Clang | MSVC |
85+
|-----|-------|------|
86+
| 8 | 7 | 19.12 |
87+
88+
## 另见
89+
90+
- [教程:std::filesystem](../../vol2-modern-features/ch09-filesystem/01-filesystem-path.md)
91+
- [cppreference: std::filesystem](https://en.cppreference.com/w/cpp/filesystem)
92+
93+
---
94+
95+
*部分内容参考自 [cppreference.com](https://en.cppreference.com/),采用 [CC-BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) 许可*
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
title: "std::format"
3+
description: "类型安全、可扩展的格式化输出库,替代 printf 和 stringstream"
4+
chapter: 99
5+
order: 7
6+
tags:
7+
- host
8+
- cpp-modern
9+
- beginner
10+
difficulty: beginner
11+
cpp_standard: [20, 23]
12+
---
13+
14+
<!--
15+
参考卡模板 (Reference Card Template)
16+
用于 documents/cpp-reference/ 下的特性速查页。
17+
与 article-template.md 不同,参考卡走精炼的结构化格式,不需要叙事风格。
18+
19+
标签使用规则:
20+
1. 必须包含 1 个 platform 标签(参考卡统一用 host)
21+
2. 必须包含 1 个 difficulty 标签
22+
3. 至少包含 1 个 topic 标签
23+
4. 从 scripts/validate_frontmatter.py 的 VALID_TAGS 集合中选取
24+
-->
25+
26+
# std::format(C++20)
27+
28+
## 一句话
29+
30+
类型安全的 `printf` 替代品——用 `{}` 占位符格式化字符串,编译期检查参数数量,支持自定义类型格式化。
31+
32+
## 头文件
33+
34+
`#include <format>`
35+
36+
## 核心 API 速查
37+
38+
| 操作 | 签名 | 说明 |
39+
|------|------|------|
40+
| 格式化字符串 | `string format(fmt, args...)` | 返回格式化后的字符串 |
41+
| 格式化到输出 | `void vformat_to(out_it, fmt, args)` | 输出到迭代器 |
42+
| 格式化到缓冲区 | `size_t formatted_size(fmt, args...)` | 预计算输出长度 |
43+
| 格式化到 stdout | (C++23) `void print(fmt, args...)` | 直接输出到标准输出 |
44+
| 位置参数 | `"{0} {1} {0}"` | 按序号引用参数 |
45+
| 宽度/精度 | `"{:>10.2f}"` | 右对齐、宽度 10、精度 2 |
46+
| 自定义格式化 | `template<> struct formatter<T>` | 特化 `std::formatter` 支持自定义类型 |
47+
48+
## 最小示例
49+
50+
```cpp
51+
// Standard: C++20
52+
#include <format>
53+
#include <iostream>
54+
#include <string>
55+
56+
int main() {
57+
std::string s = std::format("Hello, {}!", "world");
58+
std::cout << s << "\n"; // Hello, world!
59+
60+
int version = 2;
61+
double pi = 3.14159265;
62+
std::cout << std::format("v{}. pi={:.2f}", version, pi) << "\n";
63+
// v2. pi=3.14
64+
65+
// 位置参数
66+
std::cout << std::format("{0} + {0} = {1}", 3, 6) << "\n";
67+
// 3 + 3 = 6
68+
}
69+
```
70+
71+
## 嵌入式适用性:中
72+
73+
- 替代 `printf`,消除格式字符串与参数类型不匹配的运行时崩溃风险
74+
- 替代 `std::stringstream`,避免堆分配开销
75+
- 编译期检查参数数量,但格式说明符的完整编译期验证需要 C++23 的 `std::is_constant_evaluated` 配合
76+
- Flash 开销可能较大(格式化引擎代码量),资源极度受限设备需评估
77+
- 可用 [{fmt}](https://github.com/fmtlib/fmt) 库作为 C++11 起的后备方案
78+
79+
## 编译器支持
80+
81+
| GCC | Clang | MSVC |
82+
|-----|-------|------|
83+
| 13 | 17 | 19.29 |
84+
85+
## 另见
86+
87+
- [cppreference: std::format](https://en.cppreference.com/w/cpp/utility/format)
88+
89+
---
90+
91+
*部分内容参考自 [cppreference.com](https://en.cppreference.com/),采用 [CC-BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) 许可*
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
title: "std::flat_map"
3+
description: "基于连续存储的有序关联容器,缓存友好的 std::map 替代品"
4+
chapter: 99
5+
order: 8
6+
tags:
7+
- host
8+
- cpp-modern
9+
- beginner
10+
difficulty: beginner
11+
cpp_standard: [23]
12+
---
13+
14+
<!--
15+
参考卡模板 (Reference Card Template)
16+
用于 documents/cpp-reference/ 下的特性速查页。
17+
与 article-template.md 不同,参考卡走精炼的结构化格式,不需要叙事风格。
18+
19+
标签使用规则:
20+
1. 必须包含 1 个 platform 标签(参考卡统一用 host)
21+
2. 必须包含 1 个 difficulty 标签
22+
3. 至少包含 1 个 topic 标签
23+
4. 从 scripts/validate_frontmatter.py 的 VALID_TAGS 集合中选取
24+
-->
25+
26+
# std::flat_map(C++23)
27+
28+
## 一句话
29+
30+
用连续数组替代红黑树的有序映射——查找更快(缓存友好),内存更紧凑,但插入/删除是 O(n)。
31+
32+
## 头文件
33+
34+
`#include <flat_map>`
35+
36+
## 核心 API 速查
37+
38+
| 操作 | 签名 | 说明 |
39+
|------|------|------|
40+
| 访问元素 | `V& operator[](const K& key)` | 按键访问,不存在则插入默认值 |
41+
| 查找 | `iterator find(const K& key)` | 返回指向元素的迭代器 |
42+
| 插入 | `pair<iterator, bool> insert(const value_type&)` | 插入键值对 |
43+
| 删除 | `size_t erase(const K& key)` | 按键删除元素 |
44+
| 元素数量 | `size_t size() const` | 返回元素个数 |
45+
| 是否为空 | `bool empty() const` | 检查是否为空 |
46+
| 清空 | `void clear()` | 清除所有元素 |
47+
| 迭代 | `iterator begin()` / `end()` | 按键序遍历 |
48+
| 下界/上界 | `iterator lower_bound(const K&)` | 有序查找边界 |
49+
| 是否包含 | `bool contains(const K& key) const` | (C++20 起可用) 检查键是否存在 |
50+
51+
## 最小示例
52+
53+
```cpp
54+
// Standard: C++23
55+
#include <flat_map>
56+
#include <iostream>
57+
58+
int main() {
59+
std::flat_map<int, const char*> m;
60+
m[1] = "one";
61+
m[3] = "three";
62+
m[2] = "two";
63+
64+
for (const auto& [k, v] : m) {
65+
std::cout << k << ": " << v << "\n";
66+
}
67+
// 1: one 2: two 3: three (按键序排列)
68+
69+
std::cout << std::boolalpha << m.contains(2) << "\n"; // true
70+
}
71+
```
72+
73+
## 嵌入式适用性:中
74+
75+
- 连续存储对 CPU 缓存友好,小数据集的查找性能远优于 `std::map`
76+
- 无节点分配器开销,内存碎片更少,适合堆空间受限的嵌入式环境
77+
- 插入/删除 O(n),不适合频繁修改的大数据集
78+
- 编译器支持尚在推进中(GCC 15+、Clang 20+、MSVC 19.51+),生产环境需评估工具链
79+
80+
## 编译器支持
81+
82+
| GCC | Clang | MSVC |
83+
|-----|-------|------|
84+
| 15 | 20 | 19.51 |
85+
86+
## 另见
87+
88+
- [cppreference: std::flat_map](https://en.cppreference.com/w/cpp/container/flat_map)
89+
90+
---
91+
92+
*部分内容参考自 [cppreference.com](https://en.cppreference.com/),采用 [CC-BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) 许可*

0 commit comments

Comments
 (0)