C++11的新特性有哪些?

C++11,也称为C++0x或C++11标准,是C++语言在2011年发布的一个重要版本。这个版本在C++98的基础上进行了大量改进,引入了约140个新特性,并修正了约600个语言缺陷。C++11的改进使得代码更加简洁、可读、可维护,并提高了开发效率。以下是对C++11部分新特性的详细讲解及代码示例。

1. 统一初始化(Uniform Initialization)

C++11引入了统一初始化,使用花括号{}来初始化对象。这种方式减少了歧义,避免了某些常见的初始化错误,并提供了一致的语法。

代码示例

#include <iostream>
class Foo {
public:
    Foo(int) { std::cout << "Foo constructed with int\n"; }
private:
    Foo(const Foo&); // 私有拷贝构造函数
};

int main() {
    Foo a1(123); // 直接初始化
    // Foo a2 = 123; // 错误,无法使用私有拷贝构造函数
    Foo a3 = {123}; // 列表初始化,使用的是构造函数
    Foo a4{123}; // 另一种列表初始化
    int a5 = {3}; // 列表初始化,基本数据类型
    int a6{3}; // 另一种列表初始化
    return 0;
}
2. auto类型推导

C++11中,可以使用auto关键字来根据变量初始化表达式类型推导变量的实际类型,从而简化代码书写。

代码示例

#include <iostream>
#include <vector>

int main() {
    std::vector<std::vector<int>> v = {{1, 2, 3}, {4, 5, 6}};
    // 传统的迭代器定义
    std::vector<std::vector<int>>::iterator it = v.begin();
    // 使用auto进行类型推导
    auto it_auto = v.begin(); // 更简洁的写法

    // 输出结果
    for (auto inner : *it_auto) {
        std::cout << inner << " "; // 输出 1 2 3
    }
    std::cout << std::endl;

    auto x = 5; // x 是 int
    auto y = 3.14; // y 是 double
    return 0;
}
3. nullptr

C++11引入了nullptr关键字,用于取代C++03中的NULL。nullptr的类型是nullptr_t,可以更加明确地表示空指针。

代码示例

#include <iostream>

void foo(int i) {
    std::cout << "foo_int" << std::endl;
}

void foo(char* pc) {
    std::cout << "foo_char*" << std::endl;
}

int main() {
    // foo(NULL); // 不明确的
    foo(nullptr); // 调用 foo(char *)
    return 0;
}
4. enum class

C++11引入了enum class,用于解决传统enum的作用域和类型安全问题。enum class提供了更强的类型检查和作用域控制。

代码示例

#include <iostream>

enum class Apple { green, red };
enum class Orange { big, small };

int main() {
    Apple a = Apple::green;
    Orange o = Orange::big;

    if (a == o) {
        std::cout << "绿苹果 = 大橘子" << std::endl;
    } else {
        std::cout << "绿苹果 != 大橘子" << std::endl;
    }

    return 0;
}
5. Lambda表达式

C++11引入了lambda表达式,使得匿名函数的定义和使用变得更加方便。lambda表达式可以捕获外部变量,并可以像普通函数一样被调用。

代码示例

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

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

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

    // 输出排序后的结果
    for (auto number : numbers) {
        std::cout << number << " ";
    }
    std::cout << std::endl;

    return 0;
}
6. 智能指针

C++11引入了新的智能指针类,包括std::unique_ptr、std::shared_ptr和std::weak_ptr,用于更安全和方便地管理动态分配的资源,避免内存泄漏和悬空指针等问题。

代码示例

#include <iostream>
#include <memory>

int main() {
    // 使用std::unique_ptr
    std::unique_ptr<int> uniquePtr = std::make_unique<int>(42);
    std::cout << "Unique Ptr: " << *uniquePtr << std::endl;

    // 使用std::shared_ptr
    std::shared_ptr<int> sharedPtr = std::make_shared<int>(42);
    std::cout << "Shared Ptr: " << *sharedPtr << std::endl;

    // 使用std::weak_ptr解决循环引用问题
    std::shared_ptr<int> sharedPtr2 = std::make_shared<int>(42);
    std::weak_ptr<int> weakPtr(sharedPtr2);
    std::cout << "Weak Ptr (use_count of sharedPtr2): " << sharedPtr2.use_count() << std::endl;

    return 0;
}
7. 并发支持

C++11引入了新的内存模型,并提供了对多线程并发编程的支持,包括线程(std::thread)、互斥锁(std::mutex)、条件变量(std::condition_variable)等。

代码示例

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>

std::mutex mtx;
std::condition_variable cv;
bool ready = false;

void print_id(int id) {
    std::unique_lock<std::mutex> lck(mtx);
    while (!ready) cv.wait(lck);
    // 当ready为true时,继续执行
    std::cout << "Thread " << id << '\n';
}

void go() {
    std::unique_lock<std::mutex> lck(mtx);
    ready = true;
    cv.notify_all();
}

int main() {
    std::thread threads[10];
    // 启动10个线程
    for (int i = 0; i < 10; ++i)
        threads[i] = std::thread(print_id, i);

    std::cout << "10 threads ready to race...\n";
    go(); // 允许10个线程继续执行

    for (auto& th : threads) th.join(); // 等待所有线程完成

    return 0;
}

C++11的新特性还包括static_assert、override/final、constexpr、右值引用、移动语义等,这些特性共同提升了C++语言的表达能力和开发效率。通过学习和掌握这些新特性,程序员可以编写出更加简洁、高效、安全的C++代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Risehuxyc

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

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

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

打赏作者

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

抵扣说明:

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

余额充值