Ch 17: 参考资源
- 获取可靠的 C++ 参考资料
- 了解推荐的学习路径
- 掌握常用速查表
- 发现社区和工具资源
17.1 官方文档
Section titled “17.1 官方文档”17.1.1 最权威的参考资料
Section titled “17.1.1 最权威的参考资料”| 资源 | 链接 | 说明 |
|---|---|---|
| cppreference | https://en.cppreference.com/ | C++ 标准库最权威参考 |
| ISO C++ | https://isocpp.org/ | C++ 标准官方来源 |
| C++ Core Guidelines | https://isocpp.github.io/CppCoreGuidelines/ | Bjarne Stroustrup 和 Herb Sutter 编写的最佳实践指南 |
| C++ Working Papers | https://wg21.link/ | C++ 标准委员会工作文档 |
17.1.2 cppreference 使用指南
Section titled “17.1.2 cppreference 使用指南”cppreference 是 C++ 开发者最重要的参考资料:
cppreference 结构:├── C++ 教程│ └── 入门教程(覆盖基础)├── 语言参考│ ├── 关键词│ ├── 预处理器│ ├── 表达式│ └── 语句├── 标准库参考│ ├── <algorithm>│ ├── <vector>│ ├── <string>│ └── ...(所有 STL 容器和函数)└── 工具和扩展查找函数用法:
1. 打开 cppreference.com2. 在搜索框输入函数名(如 std::sort)3. 查看函数签名、参数说明、返回值4. 阅读"Possible implementation"了解实现5. 查看示例代码17.1.3 C++ Core Guidelines 核心要点
Section titled “17.1.3 C++ Core Guidelines 核心要点”C++ Core Guidelines 提供了现代 C++ 编程的权威指导:
// 来自 Core Guidelines 的重要规则示例
// P:必须使用初始化int x = 0; // ✓ 正确// int x; // ✗ 错误:未初始化
// C.47:优先使用类初始化列表class Widget { int member_{0}; // ✓ 默认成员初始化器};
// C.80-81:使用 std::variant 代替联合体// C.81:使用 std::variant(带类型安全的联合体)
// ES.20:总是初始化对象int a = 0;int* p = nullptr;
// ES.21:不要在变量定义之间引入可能抛出异常的操作// ✗ 错误int x = 42; // OKint y = function(); // 可能抛异常int z = x + y; // 这里初始化时 y 可能已构造失败
// F.50:如果函数不需要修改对象,使用 T& 或 const T&// ✓ 当需要多态或避免拷贝时使用引用
// R.10:避免 malloc 和 free// ✓ 使用 new/delete 或智能指针
// R.11:优先使用 make_unique 和 make_shared
// I.23:保持较小的函数参数数量(4个以内)17.2 推荐书籍
Section titled “17.2 推荐书籍”17.2.1 学习路径
Section titled “17.2.1 学习路径”| 阶段 | 书籍 | 适合人群 |
|---|---|---|
| 入门 | 《C++ Primer》 (Stanley Lippman) | 完全没有编程经验或从其他语言转来 |
| 入门 | 《A Tour of C++》 (Bjarne Stroustrup) | 有编程经验,想快速了解现代 C++ |
| 进阶 | 《Effective C++》 (Scott Meyers) | 已入门,想写出高质量代码 |
| 进阶 | 《Effective Modern C++》 (Scott Meyers) | 使用 C++11/14/17,想掌握现代特性 |
| 高级 | 《Effective STL》 (Scott Meyers) | 想精通 STL |
| 高级 | 《C++ Concurrency in Action》 (Anthony Williams) | 想掌握多线程编程 |
| 高级 | 《The C++ Programming Language》 (Bjarne Stroustrup) | 全面深入理解 C++ |
17.2.2 核心书籍介绍
Section titled “17.2.2 核心书籍介绍”《C++ Primer》:
- 全面覆盖 C++ 语言和标准库
- 包含 C++11/14/17 新特性
- 每章有练习题
- 适合系统学习
《Effective Modern C++》:
// 来自本书的关键条款示例
// Item 5: 优先使用 auto 而非显式类型声明auto i = 42; // ✓ 自动推导int j = 42; // 冗余
// Item 21: 优先使用 std::make_unique 和 std::make_sharedauto up = std::make_unique<int>(42); // ✓ 推荐std::unique_ptr<int> up2(new int(42)); // 不推荐
// Item 31: 避免默认捕获模式// lambda 捕获 thisclass Widget {public: void add() { // ✗ 默认按引用捕获 this(危险) auto f = [=] { return x_; }; // 实际上捕获了 this->x_
// ✓ 使用显式捕获 auto f2 = [self = this] { return self->x_; }; }private: int x_;};《C++ Concurrency in Action》:
// 来自本书的线程安全示例#include <mutex>#include <stdexcept>
class ThreadSafeCounter {private: int count_ = 0; mutable std::mutex mtx_;
public: int get() const { std::lock_guard<std::mutex> lock(mtx_); return count_; }
void increment() { std::lock_guard<std::mutex> lock(mtx_); ++count_; }};17.3 在线资源
Section titled “17.3 在线资源”17.3.1 学习平台
Section titled “17.3.1 学习平台”| 平台 | 链接 | 说明 |
|---|---|---|
| learncpp.com | https://www.learncpp.com/ | 免费 C++ 教程,适合入门 |
| cppreference | https://en.cppreference.com/ | 标准库参考 |
| Stack Overflow | https://stackoverflow.com/questions/tagged/c%2b%2b | 问题解答 |
| C++ Subreddit | https://reddit.com/r/cpp | 社区讨论 |
| C++ Discord | https://discord.gg/cpp | 实时社区 |
17.3.2 在线编译器
Section titled “17.3.2 在线编译器”| 工具 | 链接 | 用途 |
|---|---|---|
| Compiler Explorer | https://godbolt.org/ | 在线查看汇编输出 |
| C++ Insights | https://cppinsights.io/ | 可视化模板推导 |
| Wandbox | https://wandbox.org/ | 多编译器在线运行 |
| C++ Shell | https://cpp.sh/ | 快速实验代码 |
17.3.3 Compiler Explorer 使用技巧
Section titled “17.3.3 Compiler Explorer 使用技巧”Compiler Explorer(godbolt.org)是理解 C++ 代码生成的神器:
// 输入代码#include <vector>
std::vector<int> filter_even(const std::vector<int>& v) { std::vector<int> result; for (int x : v) { if (x % 2 == 0) result.push_back(x); } return result;}
// 查看编译器输出:// 1. 选择编译器(GCC、Clang、MSVC)// 2. 查看生成的汇编代码// 3. 比较不同优化级别(-O0 vs -O2 vs -O3)// 4. 理解代码实际执行17.4 标准库速查表
Section titled “17.4 标准库速查表”17.4.1 容器选择决策树
Section titled “17.4.1 容器选择决策树”需要顺序存储?├─ 是 → 主要在哪里插入/删除?│ ├─ 尾部 → std::vector│ ├─ 两端 → std::deque│ └─ 中间 → std::list 或 std::vector(考虑移动成本)└─ 否 → 需要键值映射? ├─ 是 → 需要有序键? │ ├─ 是 → std::map │ └─ 否 → std::unordered_map └─ 否 → 需要唯一性? ├─ 是 → 需要有序? │ ├─ 是 → std::set │ └─ 否 → std::unordered_set └─ 否 → 固定大小? └─ 是 → std::array 或 std::tuple17.4.2 容器对比表
Section titled “17.4.2 容器对比表”| 容器 | 随机访问 | 插入头部 | 插入尾部 | 中间插入 | 查找 | 内存连续 |
|---|---|---|---|---|---|---|
std::vector | O(1) | O(n) | O(1)* | O(n) | O(n) | ✓ |
std::deque | O(1) | O(1) | O(1) | O(n) | O(n) | ✗ |
std::list | O(n) | O(1) | O(1) | O(1) | O(n) | ✗ |
std::set | - | - | - | O(log n) | O(log n) | ✗ |
std::unordered_set | - | - | - | O(1)* | O(1) | ✗ |
std::map | - | - | - | O(log n) | O(log n) | ✗ |
std::unordered_map | - | - | - | O(1)* | O(1) | ✗ |
*均摊复杂度
17.4.3 算法复杂度速查
Section titled “17.4.3 算法复杂度速查”| 操作 | std::vector | std::list | std::set | std::unordered_set |
|---|---|---|---|---|
| 查找 | O(n) | O(n) | O(log n) | O(1) 均摊 |
| 插入 | O(n)* | O(1) | O(log n) | O(1) 均摊 |
| 删除 | O(n) | O(1)** | O(log n) | O(1) 均摊 |
| 排序迭代 | O(n log n) | O(n log n) | O(n) | O(n) |
| 顺序遍历 | O(n) | O(n) | O(n) | O(n) |
*均摊复杂度 **给定迭代器位置
17.4.4 常用算法速查
Section titled “17.4.4 常用算法速查”#include <algorithm>#include <vector>
std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6};
// 查找auto it = std::find(v.begin(), v.end(), 4); // 找元素auto it = std::find_if(v.begin(), v.end(), [](int x) { return x > 3; });auto it = std::lower_bound(v.begin(), v.end(), 4); // 二分查找(需已排序)
// 排序std::sort(v.begin(), v.end()); // 升序std::sort(v.begin(), v.end(), std::greater<int>()); // 降序std::stable_sort(v.begin(), v.end()); // 稳定排序
// 操作std::reverse(v.begin(), v.end()); // 反转std::unique(v.begin(), v.end()); // 去重(需先排序)std::shuffle(v.begin(), v.end(), rng); // 洗牌
// 计算int sum = std::accumulate(v.begin(), v.end(), 0);int count = std::count_if(v.begin(), v.end(), [](int x) { return x > 3; });auto [min_it, max_it] = std::minmax_element(v.begin(), v.end());
// 变换std::transform(v.begin(), v.end(), v.begin(), [](int x) { return x * 2; });std::copy_if(v.begin(), v.end(), std::back_inserter(result), [](int x) { return x > 3; });17.4.5 字符串函数速查
Section titled “17.4.5 字符串函数速查”#include <string>
std::string s = " Hello, World! ";
// 大小和容量s.size(); s.length(); // 字符数s.capacity(); // 底层容量s.reserve(100); // 预分配容量s.shrink_to_fit(); // 释放多余容量
// 访问s[0]; s.front(); // 第一个字符s.back(); // 最后一个字符s.at(i); // 带边界检查
// 修改s += "extra"; // 追加s.append("more"); // 追加s.insert(0, "pre"); // 插入s.erase(5, 3); // 删除s.replace(0, 5, "replacement"); // 替换
// 查找s.find("World"); // 找子串位置s.rfind("o"); // 反向查找s.find_first_of("aeiou"); // 找任意元音s.find_last_of(".!"); // 找最后一个指定字符
// 子串s.substr(7, 5); // "World"
// 转换s.erase(remove_if(s.begin(), s.end(), ::isspace), s.end()); // 去空白std::stoi(s); std::to_string(42); // 数字转换
// C++20std::string_view sv = s | std::views::drop(2) | std::views::take(5); // 视图17.4.6 智能指针选择
Section titled “17.4.6 智能指针选择”| 场景 | 推荐 | 原因 |
|---|---|---|
| 单一所有者 | std::unique_ptr | 最安全、最高效 |
| 共享所有权 | std::shared_ptr | 引用计数 |
| 观察但不拥有 | std::weak_ptr | 避免循环引用 |
| 动态数组 | std::unique_ptr<T[]> | 数组特化 |
| 缓存 | std::weak_ptr | 临时引用 |
| 插件系统 | std::shared_ptr | 共享但可卸载 |
// 选择指南示例代码
// ✓ 单一所有权 - unique_ptrauto resource = std::make_unique<Resource>();process(std::move(resource));// resource 现在为空,process 负责释放
// ✓ 共享所有权 - shared_ptrauto cache = std::make_shared<LargeData>();auto view1 = cache;auto view2 = cache;// 所有 shared_ptr 销毁后才释放
// ✓ 打破循环 - weak_ptrclass Parent {public: std::weak_ptr<Child> child; // 不增加引用计数};
// ✓ 错误示例:循环引用// class A { std::shared_ptr<B> b; };// class B { std::shared_ptr<A> a; }; // 内存泄漏!17.4.7 lambda 速查
Section titled “17.4.7 lambda 速查”#include <algorithm>#include <vector>
std::vector<int> v = {1, 2, 3, 4, 5};
// 基本语法auto f1 = [](int x) { return x * 2; }; // 无捕获auto f2 = [x](int y) { return x + y; }; // 按值捕获 xauto f3 = [&x](int y) { x = y; return x; }; // 按引用捕获auto f4 = [=]() { return x + y; }; // 按值捕获所有auto f5 = [&]() { return x + y; }; // 按引用捕获所有
// mutableint count = 0;auto counter = [count]() mutable { return ++count; };
// 返回类型auto f6 = [](int x) -> double { return x / 2.0; };
// 标准库算法中使用std::sort(v.begin(), v.end(), [](int a, int b) { return a > b; });std::all_of(v.begin(), v.end(), [](int x) { return x > 0; });
// C++14 泛型 lambdaauto generic = [](auto x) { return x * 2; };17.4.8 现代 C++ 特性速查
Section titled “17.4.8 现代 C++ 特性速查”// C++11/14 核心特性auto x = 42; // 类型推导decltype(x) y = 0; // decltypeauto fn = []() {}; // lambdastd::make_unique<T>(); // make_uniquestd::make_shared<T>(); // make_sharedstd::move(x); // 移动语义std::thread t(fn); // 线程std::mutex m; // 互斥锁std::lock_guard lock(m); // RAII 锁
// C++17 核心特性if (auto it = m.find(key); it != m.end()) { // if initializer // 使用 it}std::optional<T>; // 可选值std::variant<T, U>; // 联合体std::string_view; // 非拥有字符串视图std::any; // 任意类型if constexpr (condition) {} // constexpr if
// C++20 核心特性auto r = v | views::filter(f); // Rangesstd::format("{}", x); // 格式化concept C = ...; // Conceptsconsteval int f() {} // 立即函数std::erase_if(v, pred); // 容器的通用 erasemodule; export module; // Modulesauto operator<=>(const T&) = default; // 宇宙飞船运算符17.5 社区与更新
Section titled “17.5 社区与更新”17.5.1 关注 C++ 发展
Section titled “17.5.1 关注 C++ 发展”| 资源 | 链接 | 更新频率 |
|---|---|---|
| C++ Weekly | YouTube: C++ Weekly | 每周 |
| CppCon | YouTube: CppCon | 年度会议 |
| Meeting C++ | YouTube: Meeting C++ | 定期 |
| C++ Standard Blog | https://wg21.link/blog | 标准更新 |
17.5.2 标准委员会文档
Section titled “17.5.2 标准委员会文档”# C++ 标准工作文档https://wg21.link/P0595R2 - constexpr std:: allocatorP1206R4 - std::ranges::toP0323R12 - std::expected- cppreference.com 是最权威的标准库参考
- C++ Core Guidelines 提供编程最佳实践
- 系统化学习推荐书籍,速查表帮助日常开发
- Compiler Explorer 助理解代码生成
- 持续关注社区和标准发展,保持知识更新
本教程完