In this C++ tutorial, you shall learn how to append a given vector to the vector using vector::insert() function, with example programs.

Append Vector to Another Vector

To append a given vector to this vector C++, we can use vector::insert() method.

The syntax of the statement to append vector b to vector a is

</>
Copy
a.insert(a.end(), b.begin(), b.end());

C++ Program

In the following program, we take two vectors in a and b. We append the vector b at the end of vector a using vector::insert() method.

main.cpp

</>
Copy
#include <iostream>
#include <vector>
using namespace std;

int main() {
   vector<string> a { "apple", "banana", "cherry" };
   vector<string> b { "mango", "guava" };
   
   a.insert(a.end(), b.begin(), b.end());
   
   for(auto& element: a) {
      cout << element << endl;
   }
}

Output

apple
banana
cherry
mango
guava

Conclusion

In this C++ Tutorial, we learned how to append a given vector to the end of the original vector, using vector::insert() method.