In this tutorial, we will discuss various methods to append vector to vector in C++.
Using insert Function
We use the insert() function to add multiple elements in a vector. It is a built-in function.
There are 3 arguments that need to be passed in the insert function the first argument determines where another vector is going to be added, the second argument determines the starting of another vector and the third argument determines the ending of the vector.
Insertion at End of Vector:
#include<iostream> #include<vector> using namespace std; int main() { vector<int> vec1{15,45,60,84}; vector<int> vec2{22,56,79,91}; cout<<"Size of vector 1 before adding the vector 2 :"<<vec1.size()<<"\n"; cout<<"Elements in the vector 1 before adding: "<<"\n"; for(int i=0;i<vec1.size();i++) { cout<<vec1[i]<<" "; } vec1.insert(vec1.end(),vec2.begin(),vec2.end()); cout<<"\nSize of vector 1 after adding the vector 2 :"<<vec1.size()<<"\n"; cout<<"Elements in the vector 1 after adding: "<<"\n"; for(int i=0;i<vec1.size();i++) { cout<<vec1[i]<<" "; } return 0; }
Output:
Size of vector 1 before adding the vector 2 :4
Elements in the vector 1 before adding:
15 45 60 84
Size of vector 1 after adding the vector 2 :8
Elements in the vector 1 after adding:
15 45 60 84 22 56 79 91
Insertion at Beginning of Vector:
#include<iostream> #include<vector> using namespace std; int main() { vector<int> vec1{15,45,60,84}; vector<int> vec2{22,56,79,91}; cout<<"Size of vector 1 before adding the vector 2 :"<<vec1.size()<<"\n"; cout<<"Elements in the vector 1 before adding: "<<"\n"; for(int i=0;i<vec1.size();i++) { cout<<vec1[i]<<" "; } vec1.insert(vec1.begin(),vec2.begin(),vec2.end()); cout<<"\nSize of vector 1 after adding the vector 2 :"<<vec1.size()<<"\n"; cout<<"Elements in the vector 1 after adding: "<<"\n"; for(int i=0;i<vec1.size();i++) { cout<<vec1[i]<<" "; } return 0; }
Output:
Size of vector 1 before adding the vector 2 :4
Elements in the vector 1 before adding:
15 45 60 84
Size of vector 1 after adding the vector 2 :8
Elements in the vector 1 after adding:
22 56 79 91 15 45 60 84
Using for Loop
We can use the for loop and iterate over vector 2 simultaneously adding its elements in vector 1.
#include<iostream> #include<vector> using namespace std; int main(){ vector<int> vec1{15,45,60,84}; vector<int> vec2{22,56,79,91}; cout<<"Size of vector 1 before adding the vector 2 :"<<vec1.size()<<"\n"; cout<<"Elements in the vector 1 before adding: "<<"\n"; for(int i=0;i<vec1.size();i++) { cout<<vec1[i]<<" "; } for(int i=0;i<vec2.size();i++) { vec1.push_back(vec2[i]); } cout<<"\nSize of vector 1 after adding the vector 2 :"<<vec1.size()<<"\n"; cout<<"Elements in the vector 1 after adding: "<<"\n"; for(int i=0;i<vec1.size();i++) { cout<<vec1[i]<<" "; } return 0; }
Output:
Size of vector 1 before adding the vector 2 :4
Elements in the vector 1 before adding:
15 45 60 84
Size of vector 1 after adding the vector 2 :8
Elements in the vector 1 after adding:
15 45 60 84 22 56 79 91
Comment down below if you have any queries or know any other way to append vector to vector in c++.
The post C++ Append Vector to Vector appeared first on The Crazy Programmer.
from The Crazy Programmer https://ift.tt/3zUJdnZ
Post a Comment