
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Append a Vector in a Vector in C++
In C++, a vector is like a array which can be used accordingly. If you want to combine (append) two vectors, you need to add all the elements of one vector to the end of another. This is called appending a vector in a vector.
To append a vector in a vector can simply be done by vector insert() method and moreover by using loops and std::move() to transfer elements.
There are different ways to append one vector into another in C++. The most common ones are:
Using insert() method
This is the easiest way. The insert() function is used to add all the elements from the second vector at the end of the first one.
Syntax
Following is the syntax is as follows:
vector1.insert(vector1.end(), vector2.begin(), vector2.end());
Example
In this example, we have initialized two vectors and then appended(add) the second vector into the first vector.
#include<iostream> #include<vector> using namespace std; int main() { vector<int>vec1 = {5,6,7,8}; vector<int>vec2 = {9,10,11,12}; vec1.insert(vec1.end(), vec2.begin(), vec2.end()); cout<<"Final vector: "; for (int i : vec1) { cout<<i<<" "; } return 0; }
Following is the output to the above program:
Final vector: 5 6 7 8 9 10 11 12
Using a loop
If you don't want to use insert() method, you can simply loop through the second vector and push each element to the first one.
Syntax
Following is the syntax as below:
for (type element : vector2) { vector1.push_back(element); }
Example
In this example, we add each element manually using push_back() function.
#include<iostream> #include<vector> using namespace std; int main() { vector<int>vec1 = {21, 13, 46}; vector<int>vec2 = {64, 12, 31}; for (int val : vec2) { vec1.push_back(val); } cout<<"Merged vector: "; for (int i : vec1) { cout<<i<<" "; } return 0; }
Following is the output to the above program:
Merged vector: 21 13 46 64 12 31
Using std::move() function
This is a advance and faster method, If you want to move the elements of second vector instead of copying them, we use std::move() function.
Syntax
Following is the syntax as below:
vector1.insert( vector1.end(), make_move_iterator(vector2.begin()), make_move_iterator(vector2.end()) );
Example
In this example, we use std::move() function to move all the elements from one vector to another.
#include<iostream> #include<vector> #include<iterator> using namespace std; int main() { vector<int> vec1 = {21, 13, 46}; vector<int> vec2 = {64, 12, 31}; vec1.insert(vec1.end(), make_move_iterator(vec2.begin()), make_move_iterator(vec2.end())); cout<<"After moving: "; for (int i : vec1) { cout<<i<<" "; } return 0; }
Following is the output to the above program:
After moving: 21 13 46 64 12 31