c++ c++11之后的std::vector

本文主要介绍c++11之后std::vector的增强与特点。

范围循环 (range-based for)

  • 简化了遍历 vector 的代码。
#include <iostream>
#include <vector>

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

移动语义支持(std::move)

  • 支持移动构造和移动赋值,减少不必要的拷贝,提高性能。
#include <iostream>
#include <vector>
#include <string>

int main() {
    std::vector<std::string> v;
    std::string s = "Hello";

    v.push_back(std::move(s)); // 将字符串移动到 vector 中
    std::cout << "Vector content: " << v[0] << std::endl;
    std::cout << "Original string: " << s << std::endl; // s 变为空
    return 0;
}

初始化列表支持(std::initializer_list)

  • 使用花括号 {} 来直接初始化 vector
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5}; // 直接初始化
    // std::vector<int> v{1, 2, 3, 4, 5};
    for (auto x : v) {
        std::cout << x << " ";
    }
    std::cout << std::endl;
    return 0;
}

智能指针与动态内存

  • 结合 std::shared_ptrstd::unique_ptrvector 可以高效管理动态对象的生命周期。
#include <iostream>
#include <vector>
#include <memory>

int main() {
    std::vector<std::shared_ptr<int>> v;
    v.push_back(std::make_shared<int>(10));
    v.push_back(std::make_shared<int>(20));

    for (const auto& ptr : v) {
        std::cout << *ptr << " ";
    }
    std::cout << std::endl;
    return 0;
}

emplace方法

  • 在容器中直接构造对象,避免拷贝,提高效率。
#include <iostream>
#include <vector>
#include <string>

int main() {
    std::vector<std::string> v;
    v.emplace_back("Hello"); // 直接构造对象
    v.emplace_back(5, 'A');  // 构造一个包含 5 个 'A' 的字符串

    for (const auto& str : v) {
        std::cout << str << " ";
    }
    std::cout << std::endl;
    return 0;
}

线程安全性

  • std::vector 本身不是线程安全的,但 C++11 引入了多线程工具(如 std::thread 和互斥锁),可以保护对 vector 的访问。
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>

std::mutex mtx;
std::vector<int> v;

void addToVector(int n) {
    std::lock_guard<std::mutex> lock(mtx); // 加锁
    for (int i = 0; i < n; ++i) {
        v.push_back(i);
    }
}

int main() {
    std::thread t1(addToVector, 10);
    std::thread t2(addToVector, 10);

    t1.join();
    t2.join();

    for (auto x : v) {
        std::cout << x << " ";
    }
    std::cout << std::endl;
    return 0;
}

支持 C++17 的 std::vector功能

  • data() 方法: 返回底层数组指针(新增:C++17 前只能通过 &vec[0] 实现)。
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};
    int* p = v.data(); // 获取底层数组指针
    for (size_t i = 0; i < v.size(); ++i) {
        std::cout << p[i] << " ";
    }
    std::cout << std::endl;
    return 0;
}
  • 支持嵌套结构遍历: 利用范围循环遍历嵌套 vector
#include <iostream>
#include <vector>

int main() {
    std::vector<std::vector<int>> matrix = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    for (const auto& row : matrix) {
        for (const auto& val : row) {
            std::cout << val << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值