C++如何拷贝vector？拷贝vector的5种方法

2021年4月4日18:23:58 发表评论 10,448 次浏览

C++如何拷贝vector？对于数组拷贝, 除了迭代方法(即运行循环以在相应索引处拷贝每个元素)外, 没有太多选择将数组拷贝到其他数组中。但是vector类有不止一种方法以更简单的方式将整个vector拷贝到其他类中，所以对于C++拷贝vector，常见的是拷贝vector的5种方法，下面我们来详细介绍这5种方法：

方法1：迭代方法

``````// C++ code to demonstrate copy of vector
// by iterative method.
#include<iostream>
#include<vector>
using namespace std;

int main()
{
// Initializing vector with values
vector< int > vect1{1, 2, 3, 4};

// Declaring new vector
vector< int > vect2;

// A loop to copy elements of
// old vector into new vector
// by Iterative method
for ( int i=0; i<vect1.size(); i++)
vect2.push_back(vect1[i]);

cout << "Old vector elements are : " ;
for ( int i=0; i<vect1.size(); i++)
cout << vect1[i] << " " ;
cout << endl;

cout << "New vector elements are : " ;
for ( int i=0; i<vect2.size(); i++)
cout << vect2[i] << " " ;
cout<< endl;

// Changing value of vector to show that a new
// copy is created.
vect1[0] = 2;

cout << "The first element of old vector is :" ;
cout << vect1[0] << endl;
cout << "The first element of new vector is :" ;
cout << vect2[0] <<endl;

return 0;
}``````

``````Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is : 2
The first element of new vector is : 1``````

方法2：使用"="赋值运算符

C++如何拷贝vector？第二种方法是使用"="赋值运算符。只需将新vector分配给旧vector即可拷贝该vector。在数组的情况下, 这种分配方式是不可能的。

``````// C++ code to demonstrate copy of vector
// by iterative method.
#include<iostream>
#include<vector>
using namespace std;

int main()
{
// Initializing vector with values
vector< int > vect1{1, 2, 3, 4};

// Declaring new vector
vector< int > vect2;

// Using assignment operator to copy one
// vector to other
vect2 = vect1;

cout << "Old vector elements are : " ;
for ( int i=0; i<vect1.size(); i++)
cout << vect1[i] << " " ;
cout << endl;

cout << "New vector elements are : " ;
for ( int i=0; i<vect2.size(); i++)
cout << vect2[i] << " " ;
cout<< endl;

// Changing value of vector to show that a new
// copy is created.
vect1[0] = 2;

cout << "The first element of old vector is :" ;
cout << vect1[0] << endl;
cout << "The first element of new vector is :" ;
cout << vect2[0] <<endl;

return 0;
}``````

``````Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is : 2
The first element of new vector is : 1``````

方法3：将vector作为构造函数传递

C++如何拷贝vector的第三种方法是使用构造函数，将vector作为构造函数的参数进行传递，最后实现C++拷贝vector。在声明vector时, 传递旧的初始化vector, 将传递的vector的元素拷贝到新声明的vector中，它们被深拷贝。

``````// C++ code to demonstrate copy of vector
// by constructor method.
#include<bits/stdc++.h>
using namespace std;

int main()
{
// Initializing vector with values
vector< int > vect1{1, 2, 3, 4};

// Declaring new vector and copying
// element of old vector
// constructor method, Deep copy
vector< int > vect2(vect1);

cout << "Old vector elements are : " ;
for ( int i=0; i<vect1.size(); i++)
cout << vect1[i] << " " ;
cout << endl;

cout << "New vector elements are : " ;
for ( int i=0; i<vect2.size(); i++)
cout << vect2[i] << " " ;
cout<< endl;

// Changing value of vector to show that a new
// copy is created.
vect1[0] = 2;

cout << "The first element of old vector is :" ;
cout << vect1[0] << endl;
cout << "The first element of new vector is :" ;
cout << vect2[0] <<endl;

return 0;
}``````

``````Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is :2
The first element of new vector is :1``````

方法4：通过使用内置函数copy

copy(first_iterator_o, last_iterator_o, back_inserter())：这是将旧vector拷贝到新vector的另一种方法。这个函数有3个参数，第一个是旧vector的第一个迭代器，第二个是旧vector的最后一个迭代器，第三个是用于从back插入值的back_inserter函数。这也生成了一个vector深拷贝。

``````// C++ code to demonstrate copy of vector
// by assign() and copy().
#include<iostream>
#include<vector> // for vector
#include<algorithm> // for copy() and assign()
#include<iterator> // for back_inserter
using namespace std;
int main()
{
// Initializing vector with values
vector< int > vect1{1, 2, 3, 4};

// Declaring new vector
vector< int > vect2;

// Copying vector by copy function
copy(vect1.begin(), vect1.end(), back_inserter(vect2));

cout << "Old vector elements are : " ;
for ( int i=0; i<vect1.size(); i++)
cout << vect1[i] << " " ;
cout << endl;

cout << "New vector elements are : " ;
for ( int i=0; i<vect2.size(); i++)
cout << vect2[i] << " " ;
cout<< endl;

// Changing value of vector to show that a new
// copy is created.
vect1[0] = 2;

cout << "The first element of old vector is :" ;
cout << vect1[0] << endl;
cout << "The first element of new vector is :" ;
cout << vect2[0] <<endl;

return 0;
}``````

``````Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is :2
The first element of new vector is :1``````

C++如何拷贝vector？下面我们来介绍拷贝vector的5种方法的最后一种方法，这是一种最简单的方法，使用上比较简洁，也是推荐使用的一种方法。

方法4：使用函数assign函数

assign(first_iterator_o, last_iterator_o)：此方法为新vector分配与旧vector相同的值。这需要2个参数, 第一个参数旧vector迭代器开始, 最后一个参数是旧vector迭代器结束, 这会生成一个vector深拷贝。

``````// C++ code to demonstrate copy of vector
// by assign()
#include<iostream>
#include<vector> // for vector
#include<algorithm> // for copy() and assign()
#include<iterator> // for back_inserter
using namespace std;

int main()
{
// Initializing vector with values
vector< int > vect1{1, 2, 3, 4};

// Declaring another vector
vector< int > vect2;

// Copying vector by assign function
vect2.assign(vect1.begin(), vect1.end());

cout << "Old vector elements are : " ;
for ( int i=0; i<vect1.size(); i++)
cout << vect1[i] << " " ;
cout << endl;

cout << "New vector elements are : " ;
for ( int i=0; i<vect2.size(); i++)
cout << vect2[i] << " " ;
cout<< endl;

// Changing value of vector to show that a new
// copy is created.
vect1[0] = 2;

cout << "The first element of old vector is :" ;
cout << vect1[0] << endl;
cout << "The first element of new vector is :" ;
cout << vect2[0] <<endl;

return 0;
}``````

``````Old vector elements are : 1 2 3 4
New vector elements are : 1 2 3 4
The first element of old vector is :2
The first element of new vector is :1``````

C++如何拷贝vector？以上就是关于C++拷贝vector的5种方法了，包含用法解析和详细示例。这拷贝vector的5种方法是我们在C++开发中常见的方法，而对vector的拷贝也是常遇到的。学会并理解这些拷贝用法可以让我们的开发更顺利，希望本文对你有所帮助，如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请在下方评论。