迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中复制矢量容器对象

作者:迹忆客 最近更新:2023/03/23 浏览次数:

本文解释了如何在 C++ 中复制 std::vector 容器对象的几种方法。

在 C++ 中使用初始化列表表示法复制向量容器对象

std::vector 是 STL 容器库中提供的核心数据结构。它实现了连续存储的动态大小的数组元素。当用户从数组中添加或删除元素时,内存管理会自动完成。在构造 std::vector 类型的新变量时,我们可以使用初始化列表符号制作向量对象的副本。请注意,我们只需要传递需要复制到新对象中的原始向量对象的 beginend 迭代器。使用相同的表示法,你可以通过将相应的迭代器指定为大括号中的参数来提取对象的任何子向量。

#include <iostream>
#include <vector>

using std::cout; using std::endl;
using std::vector; using std::copy;

template<typename T>
void printVector(std::vector<T> v) {
    for (const auto &item : v) {
        cout << item << "; ";
    }
    cout << endl;
}

int main() {
    vector<int> vec1 = { 23, 43, 324, 222,
                           649, 102, 40, 304};

    vector<int> vec1_c = {vec1.begin(), vec1.end()};
    vector<int> vec1_cc = {vec1.begin(), vec1.end()-4};

    printVector(vec1_c);
    printVector(vec1_cc);

    return EXIT_SUCCESS;
}

输出:

23; 43; 324; 222; 649; 102; 40; 304;
23; 43; 324; 222;

在 C++ 中使用 std::copy 算法复制向量容器对象

复制 std::vector 对象的另一种方法是从 STL 算法库中调用 std::copy 函数。它为基于范围的对象提供通用复制操作。该函数有多个重载,但以下代码片段中使用的重载需要三个迭代器参数。前两个指定原始向量范围,而第三个迭代器指示目标向量范围的开始。

#include <iostream>
#include <vector>

using std::cout; using std::endl;
using std::vector; using std::copy;

template<typename T>
void printVector(std::vector<T> v) {
    for (const auto &item : v) {
        cout << item << "; ";
    }
    cout << endl;
}

int main() {
    vector<int> vec1 = { 23, 43, 324, 222,
                           649, 102, 40, 304};

    vector<int> vec1_c(vec1.size());
    copy(vec1.begin(), vec1.end(), vec1_c.begin());
    printVector(vec1_c);

    return EXIT_SUCCESS;
}

输出:

23; 43; 324; 222; 649; 102; 40; 304;

在 C++ 中使用复制赋值运算符复制向量容器对象

或者,你可以使用复制赋值运算符来复制 std::vector 容器对象的内容。这个符号是这个问题最简洁的解决方案。我们只需要将原始向量的变量赋值给新声明的向量对象即可。

#include <iostream>
#include <vector>

using std::cout; using std::endl;
using std::vector; using std::copy;

template<typename T>
void printVector(std::vector<T> v) {
    for (const auto &item : v) {
        cout << item << "; ";
    }
    cout << endl;
}

int main() {
    vector<int> vec1 = { 23, 43, 324, 222,
                           649, 102, 40, 304};

    vector<int> vec1_c = vec1;
    printVector(vec1_c);

    return EXIT_SUCCESS;
}

输出:

23; 43; 324; 222; 649; 102; 40; 304;

在 C++ 中使用复制构造函数复制向量容器对象

另一方面,std::vector 类的复制构造函数提供了一种类似的方法来将向量复制到新声明的向量对象中。在这种情况下,我们需要在声明新创建的向量变量时将原始向量的变量传递到括号中。

#include <iostream>
#include <vector>

using std::cout; using std::endl;
using std::vector; using std::copy;

template<typename T>
void printVector(std::vector<T> v) {
    for (const auto &item : v) {
        cout << item << "; ";
    }
    cout << endl;
}

int main() {
    vector<int> vec1 = { 23, 43, 324, 222,
                           649, 102, 40, 304};

    vector<int> vec1_c(vec1);
    printVector(vec1_c);

    return EXIT_SUCCESS;
}

输出:

23; 43; 324; 222; 649; 102; 40; 304;

使用 assign 成员函数在 C++ 中复制向量容器对象

std::vector 容器提供了 assign 成员函数,你可以利用该函数将一个向量对象的内容替换为另一个向量的元素。请注意,我们可以初始化一个空的 vector 容器,然后从对象调用 assign 函数来复制另一个 vector 的内容。

#include <iostream>
#include <vector>

using std::cout; using std::endl;
using std::vector; using std::copy;

template<typename T>
void printVector(std::vector<T> v) {
    for (const auto &item : v) {
        cout << item << "; ";
    }
    cout << endl;
}

int main() {
    vector<int> vec1 = { 23, 43, 324, 222,
                           649, 102, 40, 304};

    vector<int> vec1_c;
    vec1_c.assign(vec1.begin(), vec1.end());
    printVector(vec1_c);

    return EXIT_SUCCESS;
}

输出:

23; 43; 324; 222; 649; 102; 40; 304;

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便