CPlusPlus - #029 C++11 新特性

C++11 新特性

1 前言

C++11 是 C++ 语言的一个重要版本,它在原有语言特性的基础上进行了大量扩展和改进。C++11 的新特性不仅提高了编程效率,还大大增强了语言的表达能力,使得 C++ 程序员能够编写出更简洁、高效和现代的代码。从智能指针到 Lambda 表达式,从自动类型推断到移动语义,C++11 引入了一系列革命性的变化,这些变化极大地改变了 C++ 的编程风格。本文将详细介绍 C++11 中的主要新特性,帮助读者深入理解并掌握这些特性,从而在实际编程中灵活运用。

2 示例代码

2.1 Auto 关键字: 自动类型推断

2.1.1 C++ 11的实现方式
#include <vector>
#include <iostream>

int main() {
    std::vector<int> vec = {1, 2, 3, 4};
    for (auto i : vec) { // auto关键字用于自动类型推断
        std::cout << i << " ";
    }
    return 0;
}
2.1.1 C++ 11之前的实现方式
#include <vector>
#include <iostream>

int main() {
    std::vector<int> vec = {1, 2, 3, 4};
    for (int i : vec) { // auto关键字用于自动类型推断
        std::cout << i << " ";
    }
    return 0;
}

2.2 Lambda 表达式: 匿名函数

2.2.1 C++ 11的实现方式
#include <algorithm>
#include <vector>
#include <iostream>

int main() {
    std::vector<int> vec = {1, 2, 3, 4};
    std::for_each(vec.begin(), vec.end(), [](int x) { std::cout << x << " "; }); // Lambda表达式,定义匿名函数
    return 0;
}

2.2.2 C++ 11之前的实现方式
2.2.2.1 使用普通函数
#include <algorithm>
#include <vector>
#include <iostream>

// 定义一个普通函数
void print(int x) {
    std::cout << x << " ";
}

int main() {
    std::vector<int> vec = {1, 2, 3, 4};
    std::for_each(vec.begin(), vec.end(), print); // 使用普通函数
    return 0;
}
2.2.2.2 使用仿函数(函数对象)
#include <algorithm>
#include <vector>
#include <iostream>

// 定义一个仿函数(函数对象)
struct Printer {
    void operator()(int x) const {
        std::cout << x << " ";
    }
};

int main() {
    std::vector<int> vec = {1, 2, 3, 4};
    std::for_each(vec.begin(), vec.end(), Printer()); // 使用仿函数
    return 0;
}

2.3 智能指针 (std::shared_ptr, std::unique_ptr): 自动内存管理

2.3.1 C++ 11的实现方式
#include <memory>
#include <iostream>

int main() {
    std::shared_ptr<int> p1 = std::make_shared<int>(10); // shared_ptr,用于共享所有权的智能指针
    std::unique_ptr<int> p2 = std::make_unique<int>(20); // unique_ptr,用于独占所有权的智能指针
    std::cout << *p1 << " " << *p2 << std::endl;
    return 0;
}
2.3.2 C++ 11之前的实现方式

在 C++11 之前,没有标准库提供的智能指针(如 std::shared_ptr 和 std::unique_ptr),我们需要手动管理内存,确保在适当的地方释放分配的内存。可以通过手动的 new 和 delete 操作来实现类似的功能。

#include <iostream>

// 定义一个简单的共享计数器类,用于模拟 shared_ptr 的引用计数功能
class SharedCount {
public:
    SharedCount() : count(new int(1)) {}

    void add_ref() {
        ++(*count);
    }

    int release() {
        return --(*count);
    }

    int get_count() const {
        return *count;
    }

private:
    int* count; // 用于存储引用计数
};

// 模拟 shared_ptr 的类
template <typename T>
class MySharedPtr {
public:
    explicit MySharedPtr(T* ptr = nullptr)
        : ptr(ptr), shared_count(new SharedCount()) {}

    MySharedPtr(const MySharedPtr& other)
        : ptr(other.ptr), shared_count(other.shared_count) {
        shared_count->add_ref();
    }

    MySharedPtr& operator=(const MySharedPtr& other) {
        if (this != &other) {
            if (shared_count->release() == 0) {
                delete ptr;
                delete shared_count;
            }
            ptr = other.ptr;
            shared_count = other.shared_count;
            shared_count->add_ref();
        }
        return *this;
    }

    ~MySharedPtr() {
        if (shared_count->release() == 0) {
            delete ptr;
            delete shared_count;
        }
    }

    T& operator*() const {
        return *ptr;
    }

    T* operator->() const {
        return ptr;
    }

private:
    T* ptr;
    SharedCount* shared_count;
};

// 模拟 unique_ptr 的类
template <typename T>
class MyUniquePtr {
public:
    explicit MyUniquePtr(T* ptr = nullptr) : ptr(ptr) {}

    MyUniquePtr(MyUniquePtr&& other) noexcept : ptr(other.ptr) {
        other.ptr = nullptr;
    }

    MyUniquePtr& operator=(MyUniquePtr&& other) noexcept {
        if (this != &other) {
            delete ptr;
            ptr = other.ptr;
            other.ptr = nullptr;
        }
        return *this;
    }

    ~MyUniquePtr() {
        delete ptr;
    }

    T& operator*() const {
        return *ptr;
    }

    T* operator->() const {
        return ptr;
    }

private:
    T* ptr;

    // 禁用拷贝构造和赋值操作
    MyUniquePtr(const MyUniquePtr&) = delete;
    MyUniquePtr& operator=(const MyUniquePtr&) = delete;
};

int main() {
    MySharedPtr<int> p1(new int(10)); // 使用自定义的 shared_ptr 类
    MyUniquePtr<int> p2(new int(20)); // 使用自定义的 unique_ptr 类
    std::cout << *p1 << " " << *p2 << std::endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

满天飞飞

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

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

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

打赏作者

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

抵扣说明:

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

余额充值