附录 B:速查表

← 返回目录

B.1 语法速查(一页纸)

// 变量与常量
int x{5};                       // 花括号初始化
auto y = 3.14;                  // 类型推断
const int Z{100};               // 不可变
constexpr int SQ(int n) { return n*n; }   // 编译期函数

// 流程控制
if (cond) {} else {}
if (auto v = f(); v > 0) {}     // C++17 带初始化 if
switch (n) { case 1: ... break; default: ... }
for (int i = 0; i < n; ++i) {}
for (auto& e : vec) {}          // 范围 for(改元素加 &)
while (cond) {}
do {} while (cond);

// 函数与 lambda
int add(int a, int b) { return a + b; }
void f(const std::string& s) {}         // 大对象传 const&
auto f = [cap](Params) -> Ret { ... };  // lambda

// 类
class X {
public:
    X(int v) : v_{v} {}         // 构造函数 + 初始化列表
    ~X() {}                     // 析构
    virtual void f() {}         // 虚函数
private:
    int v_{};
};
struct P { int x, y; };         // 纯数据用 struct

B.2 类型速查

int        4 字节整数        double  8 字节浮点(默认)
unsigned   非负整数          bool    真/假
long/long long  大整数       char    1 字节字符/小整数
float      4 字节浮点(少用)void    无类型

std::string          字符串(自动管理内存)
std::string_view     [17] 零拷贝字符串视图
std::vector<T>       动态数组(默认容器)
std::array<T,N>      固定大小数组
std::map<K,V>        有序字典   std::unordered_map 哈希字典
std::set<T>          有序集合   std::unordered_set 哈希集合
std::pair<A,B>      两个值      std::tuple<A,B,C> 多个值
std::optional<T>     [17] 可能有值
std::variant<...>    [17] 多类型之一
std::expected<T,E>   [23] 有值或有错误
std::span<T>         [20] 数组视图
std::unique_ptr<T>   独占所有权智能指针
std::shared_ptr<T>   共享所有权智能指针

B.3 常用头文件速查

<print>        println / print(C++23)
<iostream>     cin / cout(老式 IO)
<string>       std::string
<vector> <map> <set> <unordered_map> <list> <deque> <array>
<algorithm>    排序查找等算法
<ranges>       [20] 视图与 ranges 算法
<numeric>      求和 fold 等
<memory>       智能指针
<optional> <variant> <any>    [17]
<expected>                     [23]
<span>                         [20]
<filesystem>                   [17] 文件系统
<chrono>       时间日期
<thread> <mutex> <atomic>     并发
<stdexcept>    异常类型
<type_traits>  编译期类型判断
<concepts>                     [20]
<cstdint>      int64_t 等定宽整数
<cctype>       isalpha 等字符判断

B.4 常用算法一句话

sort / ranges::sort        排序
find / find_if              查找
count / count_if            计数
max_element / min_element   极值
all_of / any_of / none_of   判断
accumulate                  求和汇总(fold_left 见 C++23 节)
transform / views::transform 映射
reverse                     反转
unique(先排序)            去重
remove_if + erase           删除
iota                        生成连续整数
binary_search               二分查找(有序容器)

B.5 常见坑清单(写代码前扫一眼)

  1. 整数除法丢小数:7/2 == 3,要 3.5 就 7.0/2
  2. 整数溢出不报错:int 加过头变负数
  3. 小数别用 ==:用 |a-b| < 1e-9
  4. if (x = 5) 是赋值不是比较(应写 x == 5)
  5. 忘记 break 的 switch 穿透
  6. 越界访问 [i]:at() 安全但慢,先保证 i 合法
  7. 迭代器失效:vector 插入/删除后旧迭代器作废
  8. 悬空引用/指针:别返回局部变量的地址/引用
  9. 引用捕获的 lambda 活得更久 → 悬空
  10. 大对象传值拷贝 → 传 const&
  11. 忘检查文件/输入是否打开成功
  12. const 能加就加,改不动了总比被人误改强
  13. 全局可变变量少用
  14. 字符串+数字要 format/stoi 显式转换,别乱拼
  15. 多线程共享数据要加锁(第十五章后补充章节内容)

B.6 版本功能时间线(一页记住 C++ 历史)

C++98   (1998)类、模板、STL 成型;string 等
C++11   (2011)auto、lambda、智能指针、移动语义、
                范围 for、nullptr、constexpr、花括号初始化
        ★ 现代 C++ 的起点,本书默认你知道
C++14   (2014)泛型 lambda、返回类型推断
C++17   (2017)结构化绑定、optional/variant/any、
                if constexpr、filesystem、string_view、
                折叠表达式、inline 变量、if 初始化
        ★ 本书最低要求版本
C++20   (2020)concepts、ranges、三路比较、协程、
                module、span、chrono 升级、format
C++23   (2023)print/println、expected、flat_map、
                mdspan、fold、zip/enumerate、deducing this
C++26   (2026)进一步补全(你编译器的默认未来标准)

一句话:C++11 是分水岭;C++17 之后的新代码写法 和 90 年代的老 C++ 完全是两种语言。

B.7 推荐资源(联网时可用)