对于数组,除了迭代方法(即运行循环以在相应的索引处复制每个元素)外,没有太多选择将数组复制到其他数组中。但是Vector类有多种方法可以轻松地将整个vector复制到其他向量中,基本上有两种复制类型:
方法1:迭代方法。
此方法是一种通用的复制方法,该方法使用循环将push_back()的旧矢量元素转换为新矢量,并将其深层复制
// C++ code to demonstrate copy of vector
// by iterative method.
#include
#include
using namespace std;
int main()
{
// Initializing vector with values
vector vect1{1, 2, 3, 4};
// Declaring new vector
vector vect2;
// A loop to copy elements of
// old vector into new vector
// by Iterative method
for (int i=0; i
输出:
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++ code to demonstrate copy of vector
// by iterative method.
#include
#include
using namespace std;
int main()
{
// Initializing vector with values
vector vect1{1, 2, 3, 4};
// Declaring new vector
vector vect2;
// Using assignment operator to copy one
// vector to other
vect2 = vect1;
cout << "Old vector elements are : ";
for (int i=0; i
输出:
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++ code to demonstrate copy of vector
// by constructor method.
#include
using namespace std;
int main()
{
// Initializing vector with values
vector vect1{1, 2, 3, 4};
// Declaring new vector and copying
// element of old vector
// constructor method, Deep copy
vector vect2(vect1);
cout << "Old vector elements are : ";
for (int i=0; i
输出:
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:通过使用内置函数
- 复制(first_iterator_o,last_iterator_o,back_inserter()) :-这是将旧矢量复制到新矢量中的另一种方法。此函数有3个参数,第一个是旧矢量的第一个迭代器,第二个,旧矢量的最后一个迭代器,第三个是back_inserter函数,用于从back插入值。这也是
生成了一个深拷贝。// C++ code to demonstrate copy of vector // by assign() and copy(). #include
#include // for vector #include // for copy() and assign() #include // for back_inserter using namespace std; int main() { // Initializing vector with values vector vect1{1, 2, 3, 4}; // Declaring new vector vector vect2; // Copying vector by copy function copy(vect1.begin(), vect1.end(), back_inserter(vect2)); cout << "Old vector elements are : "; for (int i=0; i 输出 :
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
- Assign(first_iterator_o,last_iterator_o) :-此方法将与旧向量相同的值分配给新向量。它有2个参数,第一个迭代器到旧向量,最后一个迭代器到旧向量,这会生成一个深层副本。
// C++ code to demonstrate copy of vector // by assign() #include
#include // for vector #include // for copy() and assign() #include // for back_inserter using namespace std; int main() { // Initializing vector with values vector vect1{1, 2, 3, 4}; // Declaring another vector vector vect2; // Copying vector by assign function vect2.assign(vect1.begin(), vect1.end()); cout << "Old vector elements are : "; for (int i=0; i 输出:
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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。