面试题:C++11 中有哪些常用的新特性?

C++11 是 C++ 语言的一次重大更新,引入了许多新特性,极大地提升了代码的简洁性、安全性和性能。

以下是 C++11 中常用的新特性:


1. 自动类型推导(auto

  • 作用:编译器自动推导变量的类型。
  • 使用场景:简化代码,特别是在类型名较长或复杂时。
  • 示例
  auto x = 10; // x 的类型推导为 int
  auto y = 3.14; // y 的类型推导为 double

2. 范围 for 循环(Range-based for loop)

  • 作用:简化遍历容器或数组的语法。
  • 使用场景:遍历标准库容器(如 std::vectorstd::list 等)。
  • 示例
  std::vector<int> vec = {1, 2, 3, 4, 5};
  for (auto& val : vec) {
      std::cout << val << " ";
  }

3. 智能指针(Smart Pointers)

  • 作用:自动管理动态内存,避免内存泄漏。
  • 常用类型
    • std::unique_ptr:独占所有权的智能指针。
    • std::shared_ptr:共享所有权的智能指针。
    • std::weak_ptr:弱引用,解决 shared_ptr 的循环引用问题。
  • 示例
  std::unique_ptr<int> ptr = std::make_unique<int>(10);
  std::shared_ptr<int> ptr2 = std::make_shared<int>(20);

4. Lambda 表达式

  • 作用:定义匿名函数,简化代码。
  • 语法
  [capture](parameters) -> return_type { body }
  • 使用场景:需要临时函数的场景,如算法回调。
  • 示例
  auto add = [](int a, int b) { return a + b; };
  std::cout << add(3, 4) << std::endl; // 输出 7

5. 右值引用和移动语义(Rvalue Reference and Move Semantics)

  • 作用:优化资源管理,避免不必要的拷贝。
  • 关键字std::movestd::forward
  • 使用场景:实现高效的资源转移(如动态数组、文件句柄等)。
  • 示例
  std::vector<int> vec1 = {1, 2, 3};
  std::vector<int> vec2 = std::move(vec1); // 移动语义,vec1 的资源转移到 vec2

6. nullptr

  • 作用:替代 NULL,表示空指针。
  • 优点:类型安全,避免与整数 0 的混淆。
  • 示例
  int* ptr = nullptr; // 空指针

7. std::thread 多线程支持

  • 作用:标准库支持多线程编程。
  • 使用场景:并发编程。
  • 示例
  void task() {
      std::cout << "Hello from thread!" << std::endl;
  }

  int main() {
      std::thread t(task);
      t.join(); // 等待线程结束
      return 0;
  }

8. std::functionstd::bind

  • 作用
    • std::function:封装可调用对象(函数、Lambda、成员函数等)。
    • std::bind:绑定参数,生成新的可调用对象。
  • 示例
  std::function<int(int, int)> add = [](int a, int b) { return a + b; };
  std::cout << add(3, 4) << std::endl; // 输出 7

9. constexpr

  • 作用:在编译时计算常量表达式。
  • 使用场景:优化性能,减少运行时计算。
  • 示例
  constexpr int square(int x) {
      return x * x;
  }
  int result = square(10); // 编译时计算

10. std::array

  • 作用:固定大小的数组容器,比原生数组更安全。
  • 示例
  std::array<int, 5> arr = {1, 2, 3, 4, 5};
  for (auto val : arr) {
      std::cout << val << " ";
  }

11. std::unordered_mapstd::unordered_set

  • 作用:基于哈希表的关联容器,提供更高效的查找性能。
  • 示例
  std::unordered_map<std::string, int> map = {{"apple", 1}, {"banana", 2}};
  std::cout << map["apple"] << std::endl; // 输出 1

12. std::tuple

  • 作用:将多个值组合成一个对象。
  • 示例
  std::tuple<int, double, std::string> t(10, 3.14, "Hello");
  std::cout << std::get<0>(t) << std::endl; // 输出 10

13. std::chrono 时间库

  • 作用:提供高精度的时间操作。
  • 示例
  auto start = std::chrono::high_resolution_clock::now();
  // 执行一些操作
  auto end = std::chrono::high_resolution_clock::now();
  auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  std::cout << "Time: " << duration.count() << " ms" << std::endl;

14. std::initializer_list

  • 作用:支持初始化列表语法。
  • 示例
  std::vector<int> vec = {1, 2, 3, 4, 5}; // 使用初始化列表

15. overridefinal

  • 作用
    • override:显式标记重写虚函数。
    • final:禁止派生类重写虚函数或继承类。
  • 示例
  class Base {
  public:
      virtual void func() {}
  };

  class Derived : public Base {
  public:
      void func() override {} // 显式标记重写
  };

总结

C++11 引入了许多新特性,涵盖了语法简化、性能优化、并发编程等方面。以下是一些常用特性的快速回顾:

特性作用
auto自动类型推导
范围 for 循环简化容器遍历
智能指针自动管理内存,避免内存泄漏
Lambda 表达式定义匿名函数
右值引用和移动语义优化资源管理
nullptr类型安全的空指针
多线程支持标准库支持多线程编程
std::functionstd::bind封装和绑定可调用对象
constexpr编译时计算常量表达式
std::array更安全的固定大小数组
std::unordered_mapstd::unordered_set基于哈希表的关联容器
std::tuple组合多个值
std::chrono高精度时间操作
std::initializer_list支持初始化列表语法
overridefinal显式标记重写虚函数或禁止重写

掌握这些特性可以显著提升 C++ 编程效率和代码质量。

THE END
点赞8 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容