# How to concatenate multiple vectors in C++

Hello Coders! In this tutorial, we are going to how we can concatenate multiple vectors in C++.   So there are many ways to concatenate multiple vectors. Let’s have a look at a few easy and simple ways here in this post! First Let’s understand what is concatenation.

What is concatenation?

Concatenation refers to as joining of two or more vectors together in a single vector. Let’s understand this using a simple example:–

```vector X={1,2,3,4}

vector Y={5,6,7,8}

X concatenate Y = { 1,2,3,4,5,6,7,8}

```

## Method 1 : Using std::reverse and std::insert

Let’s have a look at the code—>

```#include <iostream>
#include <vector>
using namespace std;
template <typename T> //introducing template T
std::vector<T> operator+(std::vector<T> const &m, std::vector<T> const &n) //pairs of template of x and y .
{
std::vector<T> v;  //initialized vector v
v.reserve(m.size() + n.size());    //reverse function used in v
v.insert(v.end(), m.begin(), m.end());  //insert func used in vec m.
v.insert(v.end(), n.begin(), n.end());  //insert func used in vec n.
return v;                        //return the vector v
}

template <typename T> //introducing template T
std::vector<T> &operator+=(std::vector<T> &m, const std::vector<T> &n) //pairs of vector n and m.
{
m.reserve(m.size() + n.size()); // using reverse func
m.insert(m.end(), n.begin(), n.end()); //using insert func
return m;            //return whole vector m
}

int main()
{
std::vector<int> vector1 = {98, 65, 43};
std::vector<int> vector2 = {68, 23};
std::vector<int> vector3 = {78, 47, 768};

for (int &i: vector1) {
std::cout << i <<" ";  //printing the concatenated vector.
}
}```

Output:

`98 65 43 68 23 78 47 768`

Explanation of the Code:

First, we have introduced a template in our code to insert our pair class of vectors m and n. We then initialized our vector `v` . Using the reverse function we passed the values of `m` and `n` vector’s size.  Then we passed the insert function using vector `v`  and in arguments, we have given the `v.end()`, then the end and begin functions of our `m` and `n` vectors respectively.

Again, we have used vector `m` and `n` as constant in the template as pairs and then we have done operator overloading += of `m` and `n`. After that using the reverse function in `m` vector we have given arguments as m size and n size. Then we used the `insert` function and passed the arguments as this `[m.end(), n.begin(), n.end()]` . We then returned the whole concatenated vector as `m`.

In the `driver` function, we have used vector 1 and vector 2 and given its value. Then we concatenated `vector2` and `vector3` with `vector1`. After that, we used for loop for printing our concatenated vectors.

Note: Don’t forget to use the vector header file.

## Method 2: Using std::move and std::back_inserter

```#include <iostream>
#include <vector> //using vector header file.
#include <algorithm>

int main()
{
std::vector<int> one = {145, 342, 213};  //initialize vector 1
std::vector<int> two = {674, 385};  //initialize vector 2

std::move(two.begin(), two.end(), std::back_inserter(one)); //std::move and std::back_inserter

for (int &i: one) {
std::cout << i << ' ';  //print the vectors ( concatenated )
}
}```

Output:

`145 342 213 674 385`

Explanation of the Code:-

In this method first, we initialized 2 vectors as vector one and vector two and then we used `std:: move` function and in the arguments, we passed the second vector begin and end function along with the std:: back- inserter(one) which helps to allocate the spaces for the new elements in the vectors. Then we used the for loop for printing the concatenated vector.

Note: Don’t forget to use the vector header file.

We can also use `std::copy` function instead of `std::move` function the usage is almost the same and the code also remains the same.

## Merge two vectors in C++

In order to merge two vectors in C++, we will be making use of the built-in standard library function i.e., merge().

The basic idea is:
`v1={1,2,3,4}` and `v2={5,7,6}` resultant vector is `v3={1,2,3,4,5,6,7}`.

The `merge()` function can be applied easily in order to implement the above idea.

Syntax :

`merge(parameter1 begin, parameter1 end, parameter2 begin, parameter2 end, resultant res);`

Where the first four parameters can be well understood by their names. The last parameter specifies the resultant where we will store our values after the merge operation is performed.

Returns :

The `merge()` function returns the iterator to the last element which is to the resultant value i.e., res according to the above syntax.

Lets us understand it with examples.

### Merging two integer-valued vectors in C++

```// illustrating the use of merge() function
#include <bits/stdc++.h>
using namespace std;

int main()
{
// initializing the first vector A
vector<int> A = { 10,20,30,60,5,15 };

// initializing the second vector B
vector<int> B = { 50,90,45,25,65,100 };

// declaring the resultant vector C
vector<int> C(12);

// sorting both the vectors A and B
sort(A.begin(), A.end());
sort(B.begin(), B.end());

// using merge() function to merge both the vectors
merge(A.begin(), A.end(), B.begin(), B.end(), C.begin());

// Finally printing the resultant vector C values
cout << "The resultant vector C after merging the values : ";

for (int i = 0; i < C.size(); i++)
cout << C[i] << " ";
return 0;
}
```

The above piece of snippet produces the output:

`The resultant vector C after merging the values : 5 10 15 20 25 30 45 50 60 65 90 100`

### Merging two character-valued vectors in C++

```// illustrating the use of merge() function
#include <bits/stdc++.h>
using namespace std;

int main()
{
// initializing the first vector A
vector<char> A = { 'c','o','d','e' };

// initializing the second vector B
vector<char> B = { 's','p','e','e','d','y'};

// declaring the resultant vector C
vector<char> C(10);

// sorting both the vectors A and B
sort(A.begin(), A.end());
sort(B.begin(), B.end());

// using merge() function to merge both the vectors
merge(A.begin(), A.end(), B.begin(), B.end(), C.begin());

// Finally printing the resultant vector C values
cout << "The resultant vector C after merging the values : ";

for (int i = 0; i < C.size(); i++)
cout << C[i] << " ";
return 0;
}
```

The above snippet produces the output:

`The resultant vector C after merging the values: c d d e e e o p s y`