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

Jinku Hu 2023年10月12日
  1. 在 C++ 中使用初始化列表表示法复制向量容器对象
  2. 在 C++ 中使用 std::copy 算法复制向量容器对象
  3. 在 C++ 中使用复制赋值运算符复制向量容器对象
  4. 在 C++ 中使用复制构造函数复制向量容器对象
  5. 使用 assign 成员函数在 C++ 中复制向量容器对象
在 C++ 中复制矢量容器对象

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

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

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

#include <iostream>
#include <vector>

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

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::copy;
using std::cout;
using std::endl;
using std::vector;

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::copy;
using std::cout;
using std::endl;
using std::vector;

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::copy;
using std::cout;
using std::endl;
using std::vector;

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::copy;
using std::cout;
using std::endl;
using std::vector;

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;
作者: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

DelftStack.com 创始人。Jinku 在机器人和汽车行业工作了8多年。他在自动测试、远程测试及从耐久性测试中创建报告时磨练了自己的编程技能。他拥有电气/电子工程背景,但他也扩展了自己的兴趣到嵌入式电子、嵌入式编程以及前端和后端编程。

LinkedIn Facebook

相关文章 - C++ Vector