C++ Append Vector to Vector

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:

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:

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.

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++.

Leave a Comment

Your email address will not be published.