推荐答案
C++11 新增了以下主要特性:
- 自动类型推导:
auto关键字用于自动推导变量类型。 - 范围 for 循环:
for (auto& elem : container)语法简化了容器遍历。 - Lambda 表达式:允许在代码中定义匿名函数。
- 右值引用和移动语义:引入了
&&语法和std::move,支持资源的高效转移。 - 智能指针:新增了
std::unique_ptr、std::shared_ptr和std::weak_ptr,用于自动管理内存。 - nullptr:引入了
nullptr关键字,替代NULL表示空指针。 - 委托构造函数:允许一个构造函数调用另一个构造函数。
- 继承构造函数:使用
using关键字继承基类的构造函数。 - override 和 final:
override用于显式重写虚函数,final用于阻止派生类重写函数或继承类。 - 强类型枚举:引入了
enum class,提供了更强的类型安全性。 - 静态断言:
static_assert用于编译时断言检查。 - 线程支持:引入了
std::thread、std::mutex等线程相关库。 - 新的容器和算法:如
std::array、std::forward_list和std::unordered_map等。 - 变长模板:支持模板参数包,允许模板接受任意数量的参数。
- 用户定义字面量:允许用户定义自己的字面量后缀。
本题详细解读
自动类型推导
C++11 引入了 auto 关键字,允许编译器根据初始化表达式自动推导变量的类型。例如:
auto x = 42; // x 的类型被推导为 int auto y = 3.14; // y 的类型被推导为 double
这在处理复杂类型时非常有用,可以减少代码冗余。
范围 for 循环
范围 for 循环简化了对容器的遍历操作。例如:
std::vector<int> vec = {1, 2, 3, 4};
for (auto& elem : vec) {
std::cout << elem << std::endl;
}这种语法比传统的迭代器方式更加简洁。
Lambda 表达式
Lambda 表达式允许在代码中定义匿名函数。例如:
auto add = [](int a, int b) { return a + b; };
std::cout << add(2, 3) << std::endl; // 输出 5Lambda 表达式可以捕获外部变量,并且可以作为函数对象使用。
右值引用和移动语义
右值引用 (&&) 和 std::move 允许资源的高效转移,避免了不必要的拷贝。例如:
std::string str1 = "Hello"; std::string str2 = std::move(str1); // str1 的资源被转移到 str2
移动语义在处理大型对象时非常有用,可以显著提高性能。
智能指针
C++11 引入了三种智能指针:
std::unique_ptr:独占所有权的智能指针。std::shared_ptr:共享所有权的智能指针。std::weak_ptr:弱引用,不增加引用计数。
智能指针可以自动管理内存,避免内存泄漏。
nullptr
nullptr 是 C++11 引入的空指针常量,用于替代 NULL。例如:
int* ptr = nullptr;
nullptr 的类型是 std::nullptr_t,可以避免与整数类型的混淆。
委托构造函数
委托构造函数允许一个构造函数调用另一个构造函数。例如:
class MyClass {
public:
MyClass(int x) : MyClass(x, 0) {} // 委托给另一个构造函数
MyClass(int x, int y) : x(x), y(y) {}
private:
int x, y;
};这种语法可以减少代码重复。
继承构造函数
使用 using 关键字可以继承基类的构造函数。例如:
// www.javascriptcn.com code example
class Base {
public:
Base(int x) {}
};
class Derived : public Base {
public:
using Base::Base; // 继承 Base 的构造函数
};这样可以避免在派生类中重复定义构造函数。
override 和 final
override 用于显式重写虚函数,final 用于阻止派生类重写函数或继承类。例如:
// www.javascriptcn.com code example
class Base {
public:
virtual void foo() {}
};
class Derived : public Base {
public:
void foo() override {} // 显式重写
};
class FinalClass final : public Base {
public:
void foo() final {} // 阻止进一步重写
};这些关键字可以提高代码的可读性和安全性。
强类型枚举
enum class 提供了更强的类型安全性。例如:
enum class Color { Red, Green, Blue };
Color c = Color::Red;强类型枚举不会隐式转换为整数,避免了潜在的错误。
静态断言
static_assert 用于编译时断言检查。例如:
static_assert(sizeof(int) == 4, "int must be 4 bytes");
如果断言失败,编译将终止并输出错误信息。
线程支持
C++11 引入了多线程支持,包括 std::thread、std::mutex、std::condition_variable 等。例如:
std::thread t([]() { std::cout << "Hello from thread!" << std::endl; });
t.join();这些库使得多线程编程更加方便和安全。
新的容器和算法
C++11 引入了新的容器和算法,如 std::array、std::forward_list 和 std::unordered_map 等。例如:
std::array<int, 3> arr = {1, 2, 3};
std::unordered_map<std::string, int> map = {{"one", 1}, {"two", 2}};这些容器和算法提供了更多的选择和灵活性。
变长模板
变长模板允许模板接受任意数量的参数。例如:
template<typename... Args>
void print(Args... args) {
(std::cout << ... << args) << std::endl;
}这种语法使得模板更加灵活和强大。
用户定义字面量
用户定义字面量允许用户定义自己的字面量后缀。例如:
long double operator"" _km(long double x) {
return x * 1000;
}
auto distance = 5.0_km; // distance 的值为 5000.0这种语法可以增强代码的可读性和表达力。