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

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

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

方法1:迭代方法

此方法是一种通用的C++拷贝vector的方法, 该方法使用循环将push_back()的旧vector元素转换为vector, 并实现了C++ vector的深拷贝

如下代码示例演示使用迭代方法实现C++拷贝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;
  
     // 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

在上面的代码中, 更改一个vector的值不会更改另一vector的值, 因此它们不会分配在同一地址, 因此具有vector深拷贝。

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

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

如下代码示例中使用"="赋值远算符实现C++拷贝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

拷贝vector的5种方法中的第三种方法是使用vector的构造函数,这也是一种较为基本而简单的方法,下面我们一起来看看是如何实现的。

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

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

如下示例中将旧的vector传给新的vector构造函数,最终实现了C++拷贝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

除了以上方法,C++还可以如何拷贝vector?下面我们介绍C++一些内置支持的vector拷贝方法。

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

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

下面我们使用copy函数实现C++拷贝vector,其中要注意前两个函数都是旧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深拷贝。

下面我们使用assign函数实现C++拷贝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的拷贝也是常遇到的。学会并理解这些拷贝用法可以让我们的开发更顺利,希望本文对你有所帮助,如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请在下方评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: