目录

C++11 引入了许多新特性,下面我将结合具体例子详细说明这些特性,并尽可能多列举一些应用场景:


1. 自动类型推导(auto)

auto 关键字用于自动推导变量的类型,减少代码冗余。

auto x = 5; // x 的类型被推导为 int
auto y = 3.14; // y 的类型被推导为 double
auto ptr = &x; // ptr 的类型被推导为 int*

应用场景:

• 简化复杂类型的声明,如迭代器:

std::vector<int> vec = {1, 2, 3};
for (auto it = vec.begin(); it != vec.end(); ++it) {
    std::cout << *it << " ";
}

2. 基于范围的 for 循环

简化了对容器和数组的遍历。

std::vector<int> vec = {1, 2, 3, 4, 5};
for (auto& v : vec) {
    std::cout << v << " "; // 输出: 1 2 3 4 5
}

应用场景:

• 遍历数组或容器时,避免手动管理迭代器。


3. Lambda 表达式

Lambda 表达式允许定义匿名函数。

auto add = [](int x, int y) { return x + y; };
int result = add(3, 4); // result = 7

应用场景:

• 作为回调函数传递给算法:

std::vector<int> vec = {1, 2, 3, 4, 5};
std::sort(vec.begin(), vec.end(), [](int a, int b) { return a > b; });

4. 智能指针

C++11 引入了 std::unique_ptr、std::shared_ptr 和 std::weak_ptr,用于自动管理动态内存。

std::unique_ptr<int> ptr(new int(10)); // 独占指针
std::shared_ptr<int> sharedPtr = std::make_shared<int>(20); // 共享指针

应用场景:

• 避免内存泄漏,简化资源管理。


5. 右值引用和移动语义

右值引用和移动语义允许高效地转移资源,避免不必要的拷贝。

std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = std::move(v1); // v1 的资源被移动到 v2

应用场景:

• 优化性能,特别是在处理大型对象时。


6. nullptr

nullptr 用于表示空指针,替代 NULL 和 0。

int* ptr = nullptr;
if (ptr == nullptr) {
    std::cout << "ptr is null\n";
}

应用场景:

• 明确表示空指针,避免类型歧义。


7. 强类型枚举(enum class)

强类型枚举提供了更好的类型安全性和作用域。

enum class Color { Red, Green, Blue };
Color c = Color::Red;
if (c == Color::Red) {
    std::cout << "Color is Red\n";
}

应用场景:

• 避免枚举值冲突,提高代码可读性。


8. 静态断言(static_assert)

static_assert 用于在编译时进行断言检查。

static_assert(sizeof(int) == 4, "int must be 4 bytes");

应用场景:

• 确保代码在编译时满足某些条件。


9. constexpr

constexpr 用于定义编译时常量和函数。

constexpr int square(int x) { return x * x; }
constexpr int result = square(5); // result = 25

应用场景:

• 在编译时计算常量值,优化性能。


10. 委托构造函数

委托构造函数允许一个构造函数调用另一个构造函数。

class MyClass {
public:
    MyClass(int x) : MyClass(x, 0) {}
    MyClass(int x, int y) : x(x), y(y) {}
private:
    int x, y;
};

应用场景:

• 减少构造函数中的重复代码。


11. 继承构造函数

继承构造函数允许派生类继承基类的构造函数。

class Base {
public:
    Base(int x) {}
};
class Derived : public Base {
public:
    using Base::Base;
};

应用场景:

• 简化派生类的构造函数定义。


12. 变长模板(Variadic Templates)

变长模板允许定义接受任意数量参数的模板函数或类。

template<typename... Args>
void print(Args... args) {
    (std::cout << ... << args) << "\n";
}
print(1, 2.5, "hello"); // 输出: 1 2.5 hello

应用场景:

• 实现通用函数,如日志打印、格式化输出等。


13. 线程支持

C++11 引入了 <thread> 库,支持多线程编程。

#include <thread>
void task() {
    std::cout << "Hello from thread!\n";
}
int main() {
    std::thread t(task);
    t.join();
    return 0;
}

应用场景:

• 实现并发编程,提高程序性能。


14. 正则表达式

C++11 引入了 <regex> 库,支持正则表达式操作。

#include <regex>
std::string text = "Hello, 12345!";
std::regex pattern("\\d+");
if (std::regex_search(text, pattern)) {
    std::cout << "Found a number!\n";
}

应用场景:

• 字符串匹配、替换等操作。


15. 新的容器和算法

C++11 引入了新的容器(如 std::unordered_map)和算法(如 std::all_of)。

std::unordered_map<std::string, int> map = {{"apple", 1}, {"banana", 2}};
std::vector<int> vec = {1, 2, 3, 4, 5};
bool allPositive = std::all_of(vec.begin(), vec.end(), [](int x) { return x > 0; });

应用场景:

• 更高效的数据存储和操作。


以上是 C++11 的主要新特性及其应用场景,这些特性极大地提升了 C++ 的现代性和开发效率。