很多人学 C++ 会掉进两个坑:一是把它当成"带 class 的 C",只会写 printf 和数组;二是抱着语法书从头背到尾,却从没完整编译过一个多文件项目。
C++ 真正的分水岭不是语法量,而是对象生命周期:这个对象什么时候构造、什么时候析构、谁拥有它、拷贝了几次。把这条主线理顺,STL、智能指针、移动语义都会变得顺理成章。
一、先跑起来:编译、链接与第一个程序
工具链怎么选
| 平台 | 推荐编译器 | 说明 |
|---|---|---|
| Windows | MSVC(Visual Studio)或 MinGW-w64 | VS 开箱即用;MinGW 更接近 Linux 体验 |
| macOS | Clang(Xcode Command Line Tools) | xcode-select --install 即可 |
| Linux | GCC / Clang | sudo apt install build-essential |
学习阶段建议直接用 C++20,但写题或老项目里 C++11/14 也很常见,遇到不认识的写法先确认标准版本。
第一个程序
// hello.cpp
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "请输入名字: ";
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!\n";
return 0;
}编译并运行:
g++ -std=c++20 -Wall -Wextra -O2 hello.cpp -o hello
./helloTIP
初学就把 -Wall -Wextra 加上。C++ 的很多坑(未初始化变量、隐式窄化、有符号/无符号比较)编译器其实能提前警告你。
从源码到可执行文件
| 阶段 | 做什么 | 常见产物 |
|---|---|---|
| 预处理 | 展开 #include、宏、条件编译 | .i |
| 编译 | 把 C++ 翻译成汇编 | .s |
| 汇编 | 汇编转机器码 | .o / .obj |
| 链接 | 合并目标文件与库,解析符号 | 可执行文件 |
新手最常见的两类错误:
undefined reference to ...:函数声明了但没链接到定义,通常是漏了.cpp或库multiple definition of ...:函数/全局变量定义写进了头文件,被多个源文件重复包含
头文件与源文件
// math_utils.h
#pragma once
int add(int a, int b);// math_utils.cpp
#include "math_utils.h"
int add(int a, int b) {
return a + b;
}// main.cpp
#include <iostream>
#include "math_utils.h"
int main() {
std::cout << add(1, 2) << '\n';
}规则很简单:声明放头文件,定义放源文件;模板和内联函数例外,它们要放头文件。
二、基础语法:类型、引用与函数
基本类型
| 类型 | 典型大小 | 用途 |
|---|---|---|
bool | 1 字节 | 真假 |
char | 1 字节 | 字符/字节 |
int | 4 字节 | 最常用整数 |
long long | 8 字节 | 大整数(竞赛/计数) |
float / double | 4 / 8 字节 | 浮点数,默认用 double |
size_t | 平台相关 | 容器大小、下标 |
NOTE
标准只规定最小范围,不规定精确字节数。涉及二进制读写或跨平台协议时,用 <cstdint> 里的 int32_t、uint64_t。
auto、const 与 constexpr
auto n = 42; // int
const double pi = 3.14159; // 运行期常量,不可修改
constexpr int kMax = 100; // 编译期常量,可用于数组大小
auto score = 95.5; // double
const auto& name = "cpp"; // 只读引用,避免拷贝auto 只负责推导,不会帮你做类型转换。看到 auto x = a / b; 时要清楚 a、b 是什么类型,整数除法不会自动变成浮点。
初始化:优先用花括号
int a = 1;
int b(2);
int c{3}; // 推荐:列表初始化
int d{3.9}; // 编译错误:禁止窄化转换花括号初始化能挡住"double 悄悄变 int"这类事故,现代 C++ 代码里应优先使用。
引用 vs 指针
| 特性 | 引用 T& | 指针 T* |
|---|---|---|
| 是否可为空 | 不可以 | 可以(nullptr) |
| 是否可重新绑定 | 不可以 | 可以 |
| 语法 | 像普通变量 | 需要 * / -> |
| 常见用途 | 传参、返回值、别名 | 可选对象、动态内存、多态 |
void addOne(int& x) { ++x; } // 会修改实参
void print(const std::string& s); // 只读,避免拷贝
int a = 1;
int& r = a; // r 是 a 的别名
r = 5; // a 也变成 5IMPORTANT
函数参数的经验法则:小类型按值,大对象用 const T&,需要修改用 T&,需要"可能没有"才用指针。
函数与重载
int maxOf(int a, int b) { return a > b ? a : b; }
double maxOf(double a, double b) { return a > b ? a : b; }
int add(int a, int b = 10) { return a + b; }重载看的是参数列表,不看返回值。默认参数只能从右往左给,而且别和重载写出歧义。
字符串与范围 for
#include <string>
#include <iostream>
std::string s = "hello";
s += " world";
std::cout << s.size() << '\n'; // 11
for (char ch : s) {
std::cout << ch << ' ';
}std::string 会自动管理内存,不要再用 char[] 手动拼接。读一整行用 std::getline,它不会像 >> 那样在空格处截断。
三、类与对象:C++ 的核心
struct 与 class
两者几乎一样,唯一区别是默认访问权限:struct 默认 public,class 默认 private。习惯上"纯数据"用 struct,"有不变式和行为的对象"用 class。
构造、初始化列表与析构
class Buffer {
public:
explicit Buffer(std::size_t n)
: data_(new int[n]{}), size_(n) {} // 初始化列表
~Buffer() { delete[] data_; } // 析构:释放资源
std::size_t size() const { return size_; }
private:
int* data_;
std::size_t size_;
};初始化列表不是"赋值",而是在对象构造时直接初始化成员。成员按声明顺序初始化,不是按列表书写顺序。
explicit 用来阻止单参数构造函数被隐式调用,避免 Buffer b = 10; 这种意外转换。
继承与多态
#include <iostream>
#include <memory>
#include <vector>
class Shape {
public:
virtual ~Shape() = default; // 关键:虚析构
virtual double area() const = 0; // 纯虚函数 → 抽象类
};
class Circle : public Shape {
public:
explicit Circle(double r) : r_(r) {}
double area() const override { return 3.14159 * r_ * r_; }
private:
double r_;
};
class Rect : public Shape {
public:
Rect(double w, double h) : w_(w), h_(h) {}
double area() const override { return w_ * h_; }
private:
double w_, h_;
};
int main() {
std::vector<std::unique_ptr<Shape>> shapes;
shapes.push_back(std::make_unique<Circle>(2.0));
shapes.push_back(std::make_unique<Rect>(3.0, 4.0));
for (const auto& s : shapes) {
std::cout << s->area() << '\n';
}
}IMPORTANT
只要类可能被继承并通过基类指针删除,析构函数就必须是 virtual。 否则 delete basePtr 只会调用基类析构,派生类资源泄漏,属于未定义行为。
对象切片
void takeByValue(Shape s); // 危险:派生部分被切掉
void takeByRef(const Shape& s); // 正确:保留多态把派生类对象按值传给基类参数,只会拷贝基类那部分,虚函数也不再生效。多态场景一律用引用或指针。
Rule of 0 / 3 / 5
- Rule of 0:能用
std::string、std::vector、智能指针管理资源,就不写析构/拷贝/移动 - Rule of 3:如果必须手写析构、拷贝构造、拷贝赋值,三者通常都要写
- Rule of 5:再加上移动构造、移动赋值
绝大多数业务代码应该追求 Rule of 0:让标准库组件替你管资源。
四、内存与 RAII
栈 vs 堆
| 栈 | 堆 | |
|---|---|---|
| 分配方式 | 自动 | new / malloc |
| 生命周期 | 离开作用域自动销毁 | 手动释放或智能指针管理 |
| 速度 | 快 | 慢,且有碎片 |
| 大小 | 较小(通常几 MB) | 受系统内存限制 |
int a = 1; // 栈
int* p = new int(2); // 堆
delete p; // 必须手动释放
auto q = std::make_unique<int>(3); // 堆,但离开作用域自动释放RAII:C++ 最重要的思想
资源获取即初始化(Resource Acquisition Is Initialization):把资源绑定到对象生命周期上,构造时获取,析构时释放。文件、锁、socket、内存都适用。
{
std::ofstream out("log.txt");
out << "hello\n";
} // 离开作用域,文件自动关闭这就是为什么现代 C++ 很少直接写 new / delete:手写释放早晚会漏,而析构是确定会执行的。
智能指针怎么选
| 指针 | 语义 | 典型场景 |
|---|---|---|
std::unique_ptr<T> | 独占所有权,不可拷贝 | 默认首选 |
std::shared_ptr<T> | 共享所有权,引用计数 | 多个对象共同持有 |
std::weak_ptr<T> | 弱引用,不增加计数 | 打破循环引用、观察者 |
#include <memory>
auto p = std::make_unique<int>(42);
auto q = std::make_shared<std::string>("cpp");
std::weak_ptr<std::string> observer = q; // 不增加引用计数
if (auto locked = observer.lock()) {
// 对象仍然存活
}TIP
优先 make_unique / make_shared,少写 new。它们更安全(异常安全)、更简洁,make_shared 还能把控制块和对象分配在一起。
四个高频内存错误
- 悬空指针:对象已销毁,指针还在用 → 用智能指针或及时置空
- 重复释放:两个指针指向同一块
new内存,各delete一次 → 用unique_ptr - 内存泄漏:
new之后忘记delete,或异常路径跳过释放 → RAII - 越界访问:
a[n]不检查边界 → 用vector::at()(调试期)或assert
编译时加上 AddressSanitizer 能抓出大部分问题:
g++ -std=c++20 -g -fsanitize=address,undefined demo.cpp -o demo
./demo五、STL:容器、迭代器与算法
STL 由三部分组成:容器存数据、迭代器连接容器与算法、算法处理区间。
容器全景
| 分类 | 容器 | 特点 | 常用操作复杂度 |
|---|---|---|---|
| 序列 | vector | 连续内存,尾部增删快 | 随机访问 O(1),尾部插入均摊 O(1) |
| 序列 | deque | 双端队列 | 两端插入 O(1) |
| 序列 | list | 双向链表 | 插入删除 O(1),不支持随机访问 |
| 序列 | array | 固定长度数组 | 全 O(1),无堆分配 |
| 关联 | map / set | 红黑树,有序 | 查找/插入 O(log n) |
| 无序 | unordered_map / unordered_set | 哈希表 | 平均 O(1),最坏 O(n) |
| 适配器 | stack / queue / priority_queue | 对底层容器封装 | 取决于底层 |
vector:最该先学好的容器
#include <vector>
std::vector<int> v = {3, 1, 4, 1, 5};
v.push_back(9); // 尾部追加
v.pop_back(); // 删除尾部
v.reserve(100); // 预分配容量,避免反复扩容
v.resize(10); // 改变元素个数
for (int x : v) std::cout << x << ' ';扩容时 vector 会申请更大的连续内存,把旧元素搬过去,原有迭代器、指针、引用全部失效。
for (auto it = v.begin(); it != v.end(); ++it) {
if (*it == 4) {
v.erase(it); // erase 返回下一个有效迭代器
break; // 不要在 erase 后继续用旧 it
}
}map 与 unordered_map
#include <map>
#include <unordered_map>
#include <string>
std::map<std::string, int> ordered;
ordered["apple"] = 3;
std::unordered_map<std::string, int> fast;
fast["banana"] = 5;
for (const auto& [key, value] : ordered) { // C++17 结构化绑定
std::cout << key << ": " << value << '\n';
}需要有序遍历或范围查询(lower_bound)用 map;只做键值查找、追求速度用 unordered_map。
algorithm:别手写排序和查找
#include <algorithm>
#include <numeric>
#include <vector>
std::vector<int> v = {5, 2, 8, 1, 9};
std::sort(v.begin(), v.end()); // 升序
std::sort(v.begin(), v.end(), std::greater<>{}); // 降序
auto it = std::lower_bound(v.begin(), v.end(), 8);
bool found = (it != v.end() && *it == 8);
int sum = std::accumulate(v.begin(), v.end(), 0);
auto mx = std::max_element(v.begin(), v.end());lower_bound 要求区间已排序,返回第一个不小于目标值的位置,是二分查找的标准写法。
lambda:把行为当参数传
#include <algorithm>
#include <iostream>
#include <vector>
std::vector<int> v = {1, 2, 3, 4, 5, 6};
int threshold = 3;
auto it = std::find_if(v.begin(), v.end(), [threshold](int x) {
return x > threshold;
});
std::sort(v.begin(), v.end(), [](int a, int b) {
return a > b;
});[ ] 是捕获列表:[=] 按值捕获、[&] 按引用捕获、[threshold] 只捕获指定变量。按引用捕获要确保 lambda 的生命周期不超过被捕获对象。
词频统计实战
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
int main() {
std::string text = "the quick brown fox jumps over the lazy dog the end";
std::istringstream iss(text);
std::map<std::string, int> freq;
std::string word;
while (iss >> word) {
++freq[word];
}
std::vector<std::pair<std::string, int>> items(freq.begin(), freq.end());
std::sort(items.begin(), items.end(), [](const auto& a, const auto& b) {
if (a.second != b.second) return a.second > b.second;
return a.first < b.first;
});
for (const auto& [w, c] : items) {
std::cout << w << ": " << c << '\n';
}
}六、现代 C++:更安全、更简洁
| 标准 | 代表特性 |
|---|---|
| C++11 | auto、范围 for、lambda、右值引用与移动语义、智能指针、nullptr、enum class |
| C++14 | 泛型 lambda、make_unique、返回类型推导 |
| C++17 | 结构化绑定、if constexpr、std::optional、std::variant、std::string_view、文件系统 |
| C++20 | concepts、ranges、协程、std::format、三路比较 |
nullptr 与 enum class
int* p = nullptr; // 不要再用 NULL 或 0
enum class Color { Red, Green, Blue };
Color c = Color::Green; // 强类型,不会和 int 隐式互转移动语义:避免无谓拷贝
#include <string>
#include <utility>
#include <vector>
std::vector<std::string> makeNames() {
std::vector<std::string> names = {"a", "b", "c"};
return names; // NRVO:编译器直接构造到调用方,无需 move
}
std::string s = "hello";
std::string t = std::move(s); // 把 s 的资源"搬"给 t,s 进入有效但未指定状态
std::vector<std::string> v;
v.push_back(std::move(t)); // 避免一次深拷贝关键理解:std::move 本身不移动任何东西,它只是把表达式转换成右值引用,真正搬运资源的是移动构造/移动赋值。
NOTE
不要对 const 对象、函数返回的局部变量滥用 std::move。前者无法移动只能拷贝,后者会阻止返回值优化(NRVO),反而更慢。
optional、variant 与 string_view
#include <optional>
#include <string_view>
#include <variant>
std::optional<int> parsePositive(int x) {
if (x > 0) return x;
return std::nullopt;
}
std::variant<int, std::string> value = 42; // 类型安全的 union
std::string_view view = "hello world"; // 只读视图,不拥有内存std::optional 表达"可能没有值",比返回 -1 或空指针更清晰;string_view 适合只读传参,但不能保存它指向的临时字符串。
structured bindings 与 if constexpr
#include <map>
#include <string>
std::map<std::string, int> scores{{"A", 90}, {"B", 85}};
for (const auto& [name, score] : scores) {
// name、score 直接绑定到 pair 的两个成员
}
template <typename T>
std::string describe(const T& value) {
if constexpr (std::is_arithmetic_v<T>) {
return "number";
} else {
return "other";
}
}concepts(C++20)
#include <concepts>
template <std::integral T>
T twice(T x) {
return x * 2;
}比起 SFINAE 和一堆 enable_if,concepts 把"这个模板要求什么类型"写在了签名上,报错也友好得多。
七、模板入门:泛型与 STL 的底座
template <typename T>
T maxOf(const T& a, const T& b) {
return a > b ? a : b;
}
template <typename T>
class Box {
public:
explicit Box(T value) : value_(value) {}
const T& get() const { return value_; }
private:
T value_;
};std::vector<int>、std::map<std::string, int> 都是类模板的实例。模板在编译期为每组类型生成代码,所以:
- 模板的定义要放在头文件里,否则其他源文件实例化时看不到实现
- 模板错误通常很长,从报错的第一行"required from"往下找真正原因
- 不要为了炫技写过度泛化的模板,业务代码优先用标准库
八、工程化:多文件、CMake 与调试
推荐目录结构
project/
├── CMakeLists.txt
├── include/
│ └── math_utils.h
└── src/
├── math_utils.cpp
└── main.cpp最小 CMake
cmake_minimum_required(VERSION 3.20)
project(demo LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(demo src/main.cpp src/math_utils.cpp)
target_include_directories(demo PRIVATE include)
target_compile_options(demo PRIVATE -Wall -Wextra -Wpedantic)cmake -S . -B build
cmake --build build
./build/demo调试与检查工具
| 工具 | 用途 |
|---|---|
gdb / lldb | 断点、单步、查看调用栈 |
| AddressSanitizer | 越界、悬空指针、内存泄漏 |
| UBSanitizer | 有符号溢出、空指针解引用等未定义行为 |
clang-format | 统一代码风格 |
clang-tidy | 静态检查与现代化建议 |
调试版编译:
g++ -std=c++20 -g -O0 -fsanitize=address,undefined main.cpp -o main单元测试
小项目可以用 assert 起步,正式项目推荐 Catch2 或 GoogleTest。测试不是"额外工作",而是让你敢改代码的前提。
九、高频踩坑速查
| 坑 | 现象 | 正确做法 |
|---|---|---|
| 基类析构非虚 | delete 基类指针时派生资源泄漏 | 基类析构加 virtual |
| 返回局部变量引用 | 悬空引用,输出乱码或崩溃 | 返回值,或由调用方传入引用 |
vector 扩容后还用旧迭代器 | 迭代器失效,未定义行为 | 重新获取迭代器,或先 reserve |
sizeof(数组) 传参后退化 | 函数里拿到的是指针大小 | 传 std::span 或额外传长度 |
| 有符号与无符号比较 | -1 > 0u 为真 | 统一类型,开启 -Wsign-compare |
浮点数用 == | 精度误差导致比较失败 | 用 epsilon 或整数化 |
头文件里 using namespace std; | 污染所有包含者 | 只在 .cpp 内使用,或写 std:: |
endl 频繁刷新 | 大量输出变慢 | 用 '\n',需要刷新时再 std::flush |
vector<bool> | 不是真正的 bool 数组,取地址失效 | 需要时用 std::vector<char> 或 bitset |
循环引用 shared_ptr | 引用计数永不归零,内存泄漏 | 一侧改用 weak_ptr |
十、30 天练习计划
第 1~7 天:语法与编译
- 每天写 3 个小程序:输入输出、条件循环、函数与引用
- 亲手用
g++编译一次多文件项目,理解头文件与链接 - 把
-Wall -Wextra当成默认选项,消灭所有警告
第 8~14 天:类与 STL
- 实现一个
Student/Book类,练构造、析构、const成员函数 - 用
vector+sort+lambda完成成绩排名 - 用
map统计一段文本的词频,并按次数排序输出
第 15~21 天:内存与现代特性
- 把裸指针版本改写成
unique_ptr - 用
shared_ptr/weak_ptr实现一个简单的观察者 - 练习移动语义:写一个持有
vector的类,观察拷贝与移动的差异 - 开启 ASan 跑一遍自己的代码,修掉所有越界与泄漏
第 22~30 天:工程化小项目
- 用 CMake 组织多文件项目,拆出
include/与src/ - 选一个题目完成:学生成绩管理、日志分析器、简易计算器、通讯录
- 给核心函数补单元测试,用
clang-format统一风格
十一、自测(点击展开解析)
1. 下面代码输出什么?
int a = 3;
int& r = a;
r = 5;
std::cout << a;
输出:5
r 是 a 的引用(别名),对 r 赋值就是对 a 赋值。引用一旦绑定就不能改绑到其他对象。
2. 对 std::vector 调用 push_back 导致扩容后,之前保存的迭代器还能用吗?
不能。
扩容会重新申请一块更大的连续内存,把旧元素搬过去并释放旧内存。原来的迭代器、指针、引用全部失效。正确做法是扩容后重新获取迭代器,或者提前 reserve 预留足够容量。
3. 通过基类指针 delete 一个派生类对象,但基类析构函数没有声明为 virtual,会发生什么?
未定义行为,通常表现为派生类的析构函数不被调用,导致资源泄漏。
只要类可能被继承并通过基类指针删除,析构函数就必须是 virtual。现代写法是 virtual ~Base() = default;。
4. std::move(x) 会立刻把 x 的资源搬走吗?
不会。
std::move 只是一个类型转换,把 x 转成右值引用,让编译器有机会选择移动构造/移动赋值。真正"搬资源"发生在移动构造或移动赋值函数内部。转换后 x 处于有效但未指定状态,不应再依赖它的值。
5. 需要按 key 有序遍历并做范围查询,应该选 map 还是 unordered_map?
选 std::map。
map 基于红黑树,中序遍历天然有序,且支持 lower_bound / upper_bound 做范围查询,复杂度 O(log n)。unordered_map 基于哈希,平均查找 O(1),但遍历顺序不确定,也不支持有序范围查询。