现代C++,也称为C++11,是C++编程语言的最新版本,自2011年发布以来,它引入了许多新的特性和改进,使得编程更加高效和便捷。

主要特性

  • 自动类型推导:使用auto关键字自动推导变量类型。
  • 智能指针:使用std::unique_ptrstd::shared_ptrstd::weak_ptr来管理内存。
  • Lambda表达式:提供更灵活的函数对象。
  • 线程支持:通过<thread>库支持多线程编程。
  • 范围for循环:简化迭代容器元素的方式。

示例代码

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5};

    // 使用Lambda表达式排序
    std::sort(numbers.begin(), numbers.end(), [](int a, int b) { return a < b; });

    // 使用auto关键字
    for (auto& number : numbers) {
        std::cout << number << " ";
    }

    return 0;
}

扩展阅读

更多关于现代C++的信息,可以访问我们的现代C++教程

现代C++