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};

  vector1 += vector2 + vector3; //contacting vector by operator overloading.

  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 have used insert function and passed the arguments as this [m.end(), n.begin(), n.end()] . We then returned the whole concatenated vector as m.

In driver function, we have used vector 1 and vector 2 and given its value. Then we have concatenated vector 2 and vector 3 with vector 1. 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 have initialized 2 vectors as vector one and vector two and then we have used std:: move function and in the arguments we have 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.

Leave a Reply

Your email address will not be published.