一、自动类型推导
auto
关键字
auto i = 42; // i 是 int 类型
auto d = 3.14; // d 是 double 类型
auto s = "hello"; // s 是 const char* 类型
std::vector<int> vec;
auto it = vec.begin(); // it 是 std::vector<int>::iterator 类型
decltype
关键字
int x = 10;
decltype(x) y = 20; // y 的类型与 x 相同 (int)
const std::vector<int> vec;
decltype(vec.size()) size = vec.size(); // size_t 类型
二、智能指针
unique_ptr
(独占所有权)
#include <memory>
std::unique_ptr<int> p1(new int(10));
// std::unique_ptr<int> p2 = p1; // 错误,不能复制
std::unique_ptr<int> p2 = std::move(p1); // 可以移动
shared_ptr
(共享所有权)
std::shared_ptr<int> p1(new int(20));
std::shared_ptr<int> p2 = p1; // 引用计数增加
weak_ptr
(弱引用)
std::shared_ptr<int> sp(new int(30));
std::weak_ptr<int> wp = sp;
if (auto locked = wp.lock()) { // 尝试获取 shared_ptr
// 使用 locked
}
三、右值引用和移动语义
- 右值引用 (
&&
)
void process(int& x) { /* 处理左值 */ }
void process(int&& x) { /* 处理右值 */ }
int a = 10;
process(a); // 调用左值版本
process(20); // 调用右值版本
- 移动构造函数和移动赋值运算符
class MyString {
char* data;
public:
// 移动构造函数
MyString(MyString&& other) noexcept
: data(other.data) {
other.data = nullptr;
}
// 移动赋值运算符
MyString& operator=(MyString&& other) noexcept {
if (this != &other) {
delete[] data;
data = other.data;
other.data = nullptr;
}
return *this;
}
};
std::move
std::vector<std::string> v1, v2;
v1.push_back("hello");
v2.push_back(std::move(v1[0])); // 移动而非复制
四、Lambda 表达式
auto add = [](int a, int b) { return a + b; };
int sum = add(3, 4);
// 捕获列表
int x = 10;
auto print_x = [x]() { std::cout << x; };
print_x();
// mutable Lambda
auto counter = [count = 0]() mutable { return ++count; };
五、基于范围的 for 循环
std::vector<int> vec = {1, 2, 3, 4};
// 值拷贝方式
for (int x : vec) {
std::cout << x << " ";
}
// 引用方式修改
for (int& x : vec) {
x *= 2;
}
// const 引用
for (const int& x : vec) {
std::cout << x << " ";
}
六、初始化列表
- 统一初始化语法
int x{10}; // 替代 int x = 10;
std::vector<int> v{1, 2, 3}; // 初始化列表
- 类成员初始化
class MyClass {
int x = 10; // C++11 类成员初始化
std::string s{"hello"};
};
七、nullptr
void foo(int* p) {}
foo(NULL); // 可能有问题,NULL 可能是 0
foo(nullptr); // 明确表示空指针
八、强类型枚举
enum class Color { Red, Green, Blue }; // 作用域枚举
Color c = Color::Red;
// int i = Color::Red; // 错误,不能隐式转换
九、constexpr
constexpr int square(int x) {
return x * x;
}
int array[square(5)]; // 编译时计算
十、委托构造函数
cpp复制class MyClass {
int x, y;
public:
MyClass(int a) : x(a), y(0) {}
MyClass() : MyClass(0) {} // 委托给另一个构造函数
};
十一、override
和 final
class Base {
public:
virtual void foo() const;
virtual void bar() final; // 禁止派生类重写
};
class Derived : public Base {
public:
void foo() const override; // 明确表示重写
// void bar() override; // 错误,基类已声明 final
};
十二、变长模板
template<typename... Args>
void print(Args... args) {
(std::cout << ... << args) << '\n'; // C++17 折叠表达式
}
print(1, 2.5, "hello"); // 接受任意数量参数
十三、线程支持
#include <thread>
#include <mutex>
std::mutex mtx;
void task() {
std::lock_guard<std::mutex> lock(mtx);
// 临界区代码
}
int main() {
std::thread t1(task);
std::thread t2(task);
t1.join();
t2.join();
}
十四、std::function
和 std::bind
#include <functional>
int add(int a, int b) { return a + b; }
int main() {
std::function<int(int, int)> func = add;
auto bound = std::bind(func, 10, std::placeholders::_1);
int result = bound(20); // 相当于 add(10, 20)
}
十五、std::array
#include <array>
std::array<int, 5> arr = {1, 2, 3, 4, 5};
for (auto x : arr) {
std::cout << x << " ";
}
十六、std::tuple
#include <tuple>
auto t = std::make_tuple(1, 2.5, "hello");
int x = std::get<0>(t); // 获取第一个元素
十七、类型别名 (using
)
using IntVector = std::vector<int>; // 替代 typedef
template<typename T>
using MyMap = std::map<int, T>;
十八、静态断言 (static_assert
)
static_assert(sizeof(int) == 4, "int must be 4 bytes");
十九、noexcept
void foo() noexcept { // 保证不抛出异常
// ...
}
二十、原始字符串字面量
const char* path = R"(C:\Program Files\MyApp)";
const char* html = R"(
<html>
<body>Hello</body>
</html>
)";