Methods to copy in Vector

Methods to copy in Vector
Methods to copy in Vector

Vectors are sequence containers representing arrays that can change in size. Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

C++ Vector Copy Programmes: Here, we are going to learn how to copy a vector to another using two different ways to copy one vector to another.

What is Vector?
class template
std::vector
template < class T, class Alloc = allocator > class vector; // generic template

Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time and thus, vectors do not reallocate each time an element is added to the container.

Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and re-allocations, but in any case, re-allocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortised constant time complexity (see push_back).

Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way. But Vector classes have more than one methods to copy the entire vector into others in easier ways.

The ways that we are using to copy vectors in C++, are:

  • Copy one vector’s elements to another (Simple approach)
  • Copy vector by using an assignment operator
  • Copy vector 1 to vector 2 while declaring vector 2 bypassing the first vector as an argument (parameterised constructor)

1) Copy one vector’s elements to another (Simple approach)
Syntax
std::copy(first_iterator_o, last_iterator_o, back_inserter()):
first_iteratot_0 = First iterator of first vector
last_iteratot_0 = Last iterator of first vector
back_inserter() = To insert values from back

Algorithm
Begin
Declare v1 of vector type.
Initialise some values into v1 vector in the array pattern.
Declare v2 of vector type.
Call copy(v1.begin(), v1.end(), back_inserter(v2)) to copy all
elements of v1 to v2.
Print “v1 vector elements are:”.

for (int i=0;i<1.size; i++)
print the all element of v2 vector.
Print “v2 vector elements are :”.
for (int i=0;i<2.size; i++)
print the all element of v2 vector.
End.

include<iostream>

include<vector>

using namespace std;
int main()
{
//declar and initialise vector 1
vector v1{10,20,30,40,50};
//declare vector2
vector v2;

//copy v2 to v1
for(int i=0; i<v1.size(); i++){
    v2.push_back(v1[i]);
}

//printing v1 and v2
cout<<"v1 elements: ";
for(int i=0; i<v1.size(); i++){
    cout<<v1[i]<<" ";

}
cout<<endl;
cout<<“v2 elements: “;
for(int i=0; i<v2.size(); i++){
cout<<v2[i]<<” “;
}
cout<<endl;
return 0;
}

Output
v1 elements: 10 20 30 40 50
v2 elements: 10 20 30 40 50

2) Copy vector by using an assignment operator
Syntax
std::assign(first_iterator_o, last_iterator_o):
first_iteratot_0 = First iterator of first vector.
last_iteratot_0 = Last iterator of first vector.

Algorithm
Begin
Initialise a vector v1 with its elements.
Declare another vector v2.
Call assign() to copy the elements of v1 to v2.
Print the elements of v1.
Print the elements of v2.
End.

include<iostream>

include<vector>

using namespace std;
int main()
{
//declar and initialise vector 1
vector v1{10,20,30,40,50};
//declare vector2
vector v2;

//copying v1 to v2
v2 = v1;

//printing v1 and v2
cout<<"v1 elements: ";
for(int i=0; i<v1.size(); i++){
    cout<<v1[i]<<" ";
}
cout<<endl;
cout<<"v2 elements: ";
for(int i=0; i<v2.size(); i++){
    cout<<v2[i]<<" ";
}
cout<<endl; 
return 0;

//printing v1 and v2
cout<<“v1 elements: “;
for(int i=0; i<v1.size(); i++){
cout<<v1[i]<<” “;
}
cout<<endl;
cout<<“v2 elements: “;
for(int i=0; i<v2.size(); i++){
cout<<v2[i]<<” “;
}
cout<<endl;
return 0;
}
Output
v1 elements: 10 20 30 40 50
v2 elements: 10 20 30 40 50

By push_back method
Algorithm
Begin
Initialise a vector v1 with its elements.
Declare another vector v2.
Make a for loop to copy elements of the first vector into the second vector by Iterative method using push_back().
Print the elements of v1.
Print the elements of v2.
End.

Example Code

include #include // for vector

include

include// for vector

include// for copy() and assign()

include// for back_inserter

using namespace std;
int main() {
vector v1{7,6,4,5};
vector v2;
for (int i=0; i<v1.size(); i++)
v2.push_back(v1[i]);
cout << “v1 vector elements are : “;
for (int i=0; i<v1.size(); i++)
cout << v1[i] << ” “;
cout << endl;
cout << “v2 vector elements are : “;
for (int i=0; i<v2.size(); i++)
cout << v2[i] << ” “;
cout<< endl;
return 0;
}
Output
v1 vector elements are : 7 6 4 5
v2 vector elements are : 7 6 4 5

Copy vector 1 to vector 2 while declaring vector 2 by passing the first vector as an argument (parameterised constructor)
Syntax:
vector v2(v1);

include<iostream>

include<vector>

using namespace std;
int main()
{
//declar and initialise vector 1
vector v1{10,20,30,40,50};
//declare vector2 by copying vector1
vector v2(v1);

//printing v1 and v2
cout<<"v1 elements: ";
for(int i=0; i<v1.size(); i++){
    cout<<v1[i]<<" ";
}
cout<<endl;
cout<<"v2 elements: ";
for(int i=0; i<v2.size(); i++){
    cout<<v2[i]<<" ";
}
cout<<endl; 
return 0;

}
Output
v1 elements: 10 20 30 40 50
v2 elements: 10 20 30 40 50

By using inbuilt functions:
• copy(first_iterator_o, last_iterator_o, back_inserter()): This is another way to copy old vector into new one. This function takes 3 arguments, first, the first iterator of the old vector, second, the last iterator of the old vector and third is back_inserter function to insert values from the back. This also generated a deep copy.
filter_none
edit
play_arrow
brightness_5

// 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()
{
// Initialising vector with values
vector 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; 

}

// 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()
{
// Initialising vector with values
vector 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; 

}
Output:
Old vector elements are: 1 2 3 4
New vector elements are : 1 2 3 4
The first element of the old vector is:2
The first element of the new vector is:1
• assign(first_iterator_o, last_iterator_o) :- This method assigns the same values to new vector as old one. This takes 2 arguments, the first iterator to old vector and last iterator to the old vector. This generates a deep copy.
filter_none
edit
play_arrow
brightness_5

// 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()
{
// Initialising vector with values
vector 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; 

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

By Akhil Sharma