目录
前言
第一章:C++11 核心特性
1.1 自动类型推导(auto)
1.2 范围 for 循环(Range-based for loop)
1.3 Lambda 表达式
1.4 智能指针(Smart Pointers)
1.4.1 unique_ptr
1.4.2 shared_ptr
1.5 右值引用与移动语义
第二章:C++14 特性
2.1 泛型 lambda 表达式
2.2 函数返回类型推导
2.3 数字分隔符
第三章:C++17 特性
3.1 结构化绑定(Structured Binding)
3.2 if constexpr(编译期条件判断)
3.3 折叠表达式(Fold Expressions)
第四章:C++20 特性
4.1 概念(Concepts)
4.2 范围库(Ranges)
4.3 三路比较运算符(Spaceship Operator)
第五章:实战案例:现代 C++ 风格的学生管理系统
总结
前言
自 1998 年首次标准化以来,C++ 语言经历了多次重大更新。其中,2011 年发布的 C++11 标准带来了革命性的变化,几乎重塑了这门语言的编程风格。随后的 C++14、C++17、C++20 等标准在此基础上持续优化,引入了更多实用特性,使 C++ 更现代、更安全、更高效。
对于现代 C++ 开发者而言,掌握这些新特性至关重要 —— 它们不仅能大幅提升开发效率,还能写出更简洁、更易维护、性能更优的代码。
本教程将系统讲解 C++11 至 C++20 的核心特性,包括自动类型推导、智能指针、lambda 表达式、范围 for 循环等,每个特性都配有实用案例,内容格式符合 CSDN 平台规范,帮助初学者快速掌握现代 C++ 编程范式。
第一章:C++11 核心特性
C++11 是 C++ 语言的一次重大升级,引入了数十项新特性,解决了 C++ 长期存在的诸多痛点。
1.1 自动类型推导(auto)
auto关键字允许编译器根据初始值自动推导变量类型,简化代码并提高可读性。
cpp
运行
#include <iostream>
#include <vector>
using namespace std;
int main() {
// 基本类型推导
auto x = 10; // x被推导为int
auto y = 3.14; // y被推导为double
auto s = "hello"; // s被推导为const char*
// 复杂类型推导(尤其适合迭代器)
vector<int> nums = {1, 2, 3, 4};
// C++98写法:vector<int>::iterator it = nums.begin();
auto it = nums.begin(); // 自动推导为vector<int>::iterator
// 函数返回值推导(配合decltype,C++14后可直接用auto)
auto add(int a, int b) { return a + b; } // C++14及以上支持
cout << "x=" << x << ", y=" << y << ", s=" << s << endl;
return 0;
}
注意:auto不能用于函数参数类型和数组类型的推导,且必须在定义时初始化。
1.2 范围 for 循环(Range-based for loop)
简化容器或数组的遍历,无需手动管理索引或迭代器。
cpp
运行
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
// 遍历数组
int arr[] = {1, 2, 3, 4, 5};
for (int num : arr) { // 遍历arr中的每个元素
cout << num << " ";
}
cout << endl;
// 遍历容器
vector<string> fruits = {"苹果", "香蕉", "橙子"};
for (const auto& fruit : fruits) { // 使用const&避免拷贝
cout << fruit << " ";
}
cout << endl;
// 修改元素(需要引用)
for (auto& num : arr) {
num *= 2;
}
for (int num : arr) {
cout << num << " "; // 输出2 4 6 8 10
}
cout << endl;
return 0;
}
1.3 Lambda 表达式
允许在代码中定义匿名函数,特别适合作为算法的回调函数。
基本语法:
cpp
运行
[capture](parameters) -> return_type { body }
cpp
运行
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> nums = {3, 1, 4, 1, 5, 9};
// 无捕获的lambda(排序)
sort(nums.begin(), nums.end(), [](int a, int b) {
return a < b; // 升序排序
});
// 有捕获的lambda(访问外部变量)
int threshold = 3;
vector<int> filtered;
copy_if(nums.begin(), nums.end(), back_inserter(filtered),
[threshold](int num) { // 捕获threshold
return num > threshold;
});
// 输出结果
cout << "排序后:";
for (int num : nums) cout << num << " "; // 1 1 3 4 5 9
cout << "
大于" << threshold << "的元素:";
for (int num : filtered) cout << num << " "; // 4 5 9
return 0;
}
捕获方式:
[]:不捕获任何外部变量
[=]:按值捕获所有外部变量
[&]:按引用捕获所有外部变量
[x, &y]:按值捕获 x,按引用捕获 y
1.4 智能指针(Smart Pointers)
解决手动管理动态内存导致的内存泄漏和悬垂指针问题,主要包括unique_ptr、shared_ptr和weak_ptr。
1.4.1 unique_ptr
独占所有权的智能指针,同一时间只能有一个unique_ptr指向对象。
cpp
运行
#include <iostream>
#include <memory> // 智能指针头文件
using namespace std;
class MyClass {
public:
MyClass(int id) : id(id) {
cout << "MyClass(" << id << ") 构造" << endl;
}
~MyClass() {
cout << "MyClass(" << id << ") 析构" << endl;
}
void print() {
cout << "MyClass(" << id << ")" << endl;
}
private:
int id;
};
int main() {
// 创建unique_ptr(独占所有权)
unique_ptr<MyClass> ptr1(new MyClass(1));
ptr1->print(); // 调用成员函数
// 转移所有权(不能直接赋值,必须使用move)
unique_ptr<MyClass> ptr2 = move(ptr1);
if (ptr1 == nullptr) {
cout << "ptr1已为空" << endl;
}
ptr2->print();
// 超出作用域时自动释放内存
return 0;
}
运行结果:
plaintext
MyClass(1) 构造
MyClass(1)
ptr1已为空
MyClass(1)
MyClass(1) 析构
1.4.2 shared_ptr
共享所有权的智能指针,使用引用计数管理对象生命周期。
cpp
运行
#include <ios


















暂无评论内容