How to append a vector to a vector in C++
Hi guys, in this tutorial we are going to learn about several methods that will help us to append a vector to a vector in c++.
For example, we have two vectors v1 and v2 and we have to append v2 to v1.
vector<int>v1 = {4,3,2,1};
vector<int>v2 = {-1,0,7};The output should be,
v1 = {4,3,2,1,-1,0,7};Method 1: Using vector::insert function
It will copy the elements of v2 and then inserts them to the end of v1.
Code
#include<iostream>
#include<vector>
using namespace std;
int main()
{
//Given Vectors
vector<int>v1 = {4,3,2,1};
vector<int>v2 = {-1,0,7};
//insert function used to insert elements in
//range of second and third parameter of v2
//into v1 from index equal to first parameter
v1.insert(v1.end(),v2.begin(),v2.end());
//printing the concatenated vector v1
for(auto it: v1){
cout<<it<<" ";
}
return 0;
}Method 2: Using std::move and std::copy function
std::move function will just move the whole vector v2 to the end of v1. We will also use an std::back_inserter function which is to allocate space for v2 in v1 and std::copy function will also do the same thing but it will copy the elements of v2 instead of moving them.
Code
#include<iostream>
#include<vector>
using namespace std;
int main()
{
//Given Vectors
vector<int>v1 = {4,3,2,1};
vector<int>v2 = {-1,0,7};
//move function used to move all elements
//of v2 to the end of v1 by allocating
//space for them using back_inserter function
move(v2.begin(),v2.end(),back_inserter(v1));
//now using copy function
//copy(v2.begin(),v2.end(),back_inserter(v1));
//printing the concatenated vector v1
for(auto it: v1){
cout<<it<<" ";
}
return 0;
}Method 3: Using while loop and vector::push_back function
This is the most simple way in which we can achieve our goal. We just need to iterate into v2 using a while loop and using push_back function inserts elements of v2 into v1.
Code
#include<iostream>
#include<vector>
using namespace std;
int main()
{
//Given Vectors
vector<int>v1 = {4,3,2,1};
vector<int>v2 = {-1,0,7};
//here we iterated into v2 and inserted
//elements of v2 into v1.
int siz=v2.size();
while(siz){
v1.push_back(v2[v2.size()-siz]);
siz--;
}
//printing the concatenated vector v1
for(auto it: v1){
cout<<it<<" ";
}
return 0;
}Method 4: Using vector::resize function
Here we will first resize v1 to the sum of sizes of v1 and v2 and then using at() operator assign values of v2 to v1.
Code
#include<iostream>
#include<vector>
using namespace std;
int main()
{
//Given Vectors
vector<int>v1 = {4,3,2,1};
vector<int>v2 = {-1,0,7};
//resizes size of v1 into size of v1+v2
v1.resize(v1.size()+v2.size());
int siz=v2.size();
while(siz){
// assigning values of v2 to v1
v1.at(v1.size()-siz)=v2[v2.size()-siz];
siz--;
}
//printing the concatenated vector v1
for(auto it: v1){
cout<<it<<" ";
}
return 0;
}Now take a look at the output for all these codes
Output
4 3 2 1 -1 0 7
These are some of the methods to append a vector to a vector, I hope this will help you.
And you can also read some of my other blogs listed below.
Thank you.
Leave a Reply