C++11新增内容汇总

C++11 是 C++ 语言的一次重要更新,引入了大量的新特性,使 C++ 代码更 现代化、高效、安全。以下是 C++11 的主要更新内容:


1. 自动类型推导 (auto)

auto 关键字用于自动推导变量的类型。

#include <iostream>
#include <vector>

int main() {
    auto x = 42;   // x 被推导为 int
    auto y = 3.14; // y 被推导为 double

    std::vector<int> vec = {1, 2, 3};
    for (auto v : vec) {  // 自动推导类型
        std::cout << v << " ";
    }
}

2. 范围 for 循环

C++11 引入了更简洁的 for 循环语法,适用于遍历容器。

#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};
    for (int x : v) {
        std::cout << x << " ";
    }
}

3. nullptr 关键字

C++11 引入 nullptr 代替 NULL,避免 NULL 可能被解释为 0 的问题。

void foo(int) { std::cout << "int\n"; }
void foo(char*) { std::cout << "char*\n"; }

int main() {
    foo(nullptr); // 调用 foo(char*)
}

4. decltype 类型推导

decltype 用于获取表达式的类型。

int x = 10;
decltype(x) y = 20; // y 的类型与 x 相同,即 int

5. std::move 和 右值引用(Rvalue Reference)

C++11 引入 右值引用 (&&),支持 移动语义,减少不必要的拷贝,提高性能。

#include <iostream>
#include <vector>

std::vector<int> createVector() {
    std::vector<int> v = {1, 2, 3};
    return std::move(v); // 使用移动语义,避免拷贝
}

int main() {
    std::vector<int> v = createVector();
}

6. std::unique_ptrstd::shared_ptr(智能指针)

C++11 引入 std::unique_ptrstd::shared_ptr,避免手动管理动态内存。

#include <memory>
#include <iostream>

int main() {
    std::unique_ptr<int> ptr = std::make_unique<int>(42);
    std::cout << *ptr << "\n"; // 输出: 42
}

7. std::functionstd::bind

C++11 引入 std::function,可存储可调用对象(函数、lambda)。

#include <iostream>
#include <functional>

void hello() { std::cout << "Hello\n"; }

int main() {
    std::function<void()> func = hello;
    func();  // 输出: Hello
}

8. Lambda 表达式

C++11 引入 Lambda,提供更灵活的匿名函数。

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

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};
    std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
}

9. std::thread(多线程支持)

C++11 原生支持多线程编程,提供 std::thread

#include <iostream>
#include <thread>

void hello() {
    std::cout << "Hello from thread!\n";
}

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

10. std::array(固定大小数组)

std::array 作为 std::vector 之外的另一种选择,提供更安全的数组。

#include <array>
#include <iostream>

int main() {
    std::array<int, 3> arr = {1, 2, 3};
    for (int n : arr) {
        std::cout << n << " ";
    }
}

11. std::unordered_map(哈希表)

C++11 引入 哈希表,提高 map 查找速度。

#include <unordered_map>
#include <iostream>

int main() {
    std::unordered_map<std::string, int> scores = {{"Alice", 90}, {"Bob", 85}};
    std::cout << scores["Alice"] << "\n";  // 输出: 90
}

12. constexpr(编译期常量计算)

constexpr 允许在 编译期 计算值,提高效率。

constexpr int square(int x) { return x * x; }

int main() {
    constexpr int result = square(5); // 在编译期计算
}

13. enum class(强类型枚举)

enum class 避免传统 enum 作用域冲突。

enum class Color { Red, Green, Blue };

int main() {
    Color c = Color::Red;
}

14. overridefinal 关键字

override 确保正确覆盖基类的虚函数,final 禁止继承。

struct Base {
    virtual void show() {}
};

struct Derived : Base {
    void show() override {} // 确保正确覆盖
};

15. defaultdelete 关键字

  • default:显式声明默认构造函数
  • delete:禁止某些函数的调用
struct Example {
    Example() = default;  // 使用默认构造
    Example(const Example&) = delete; // 禁止拷贝构造
};

16. std::tuple

C++11 引入 std::tuple,用于存储多个不同类型的值。

#include <tuple>
#include <iostream>

int main() {
    std::tuple<int, double, std::string> data = {42, 3.14, "Hello"};
    std::cout << std::get<0>(data) << "\n";  // 输出: 42
}

总结

C++11 极大地改善了 C++ 的可用性和效率,其中 关键特性 包括:

  • 语法改进autonullptrdecltype
  • 智能指针std::unique_ptrstd::shared_ptr
  • Lambda 表达式[](){} 使代码更紧凑
  • 多线程支持std::thread
  • 新容器std::arraystd::unordered_mapstd::tuple
  • 右值引用和移动语义:提高性能
  • 强类型枚举 (enum class):避免命名冲突

C++11 是现代 C++ 的奠基版本,提升了 性能、可读性、简洁性,是值得学习和使用的重要版本!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

点云SLAM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值