C++

How to Concatenate Vectors in C++

Vector concatenation in C++ refers to the process of combining the elements of two or more vectors into a single vector. It allows you to merge the contents of multiple vectors, creating a larger vector that contains all the elements from the original vectors.

Vector concatenation in C++ is essential for merging the data, generating composite results, accommodating the dynamic data growth, improving the code efficiency, and facilitating integration with existing algorithms and libraries. It provides a versatile and powerful mechanism for handling and manipulating the collections of data in a unified and efficient manner.

By concatenating the vectors, you can avoid creating new vectors from scratch or copying the elements one by one. It provides a convenient and efficient way to merge the vectors which simplifies your code and improve its performance.

C++ provides several methods, such as the “insert” function or the range constructor, to concatenate the vectors easily and efficiently. These methods allow you to combine the vectors while maintaining their order and preserving the elements from each source vector.

You will find a comprehensive discussion of both approaches in this post.

Method 1: Utilizing the Insert Function to Concatenate Two Vectors

The “std::vector” class provides the “insert” function which allows you to insert the elements from another container at a specified position. To concatenate the vectors, you can use this function to insert the elements from one vector into another.

Here’s the step-by-step process:

#include <iostream>

#include <vector>

int main()

{

  std::vector<int> v1 = {2, 4, 8};

  std::vector<int> v2 = {12, 16, 20};

  v1.insert(v1.end(), v2.begin(), v2.end());

  std::cout <<"Concatenated result:";

  for (int n : v1) {

    std::cout << n << " ";

  }

  std::cout << std::endl;

  return 0;

}

In this example, the #include <iostream> directive is used to include the Standard Library header file <iostream>. This header provides the necessary declarations and definitions for input and output operations. After that, the #include <vector> directive is used to include the standard library header file <vector>. This header provides the necessary declarations and definitions to work with the “std::vector” container class.

The main() function is invoked. It is a function that acts as the execution’s beginning point. In the main section, we utilize the std::vector<int>. It is a container class from the C++ Standard Library that represents a dynamic array of integers. It offers a dynamic and efficient strategy to store and manipulate a sequence of elements.

So, we declare and initialize two vectors – “v1” and “v2” – using the std::vector<int>. The “v1” vector is initialized with 2, 4, and 8. Whereas the “v2” vector has the values of 12, 16, and 20. We want to concatenate “v1” and “v2”. To concatenate these vectors, we invoke the insert() function. We use the “insert” function on the destination vector to insert the elements from the source vector. Here, the destination vector is “v1” and the source vector is “v2”. This means that the “insert” function takes the values from the “v2” vector and insert them into “v1” along with the existing values of “v1”.

The “insert” function takes three arguments: the position where you want to insert the elements using the destination.end(), the beginning iterator of the range that you want to insert using the source.begin(), and the end iterator of the range that you want to insert using the source.end().

In our case, the destination vector is “v1” and the source vector is “v2”. Thus, when we call the insert function as v1.insert() and the parameters are v1.end(), v2.begin(), and v2.end(). The values from the “v2” vector from beginning to end are inserted into the end of “v1”.

The “std::cout” is then utilized to print a “Concatenated vectors:” statement. The for-loop is used to print the values of “v1” which now contains the values of “v2” as well. The condition for the loop is set as (if int n: v1). The (:) operator shows that it is a range-based for-loop.

In our example, the range-based for-loop iterates over each element in the std::vector<int> called “v1”. During each iteration, the loop variable “n” gets the value of every element in the vector. The loop body, enclosed in curly braces {}, performs an operation on each element which prints the element that employs the “std::cout”.

Upon exiting the loop, the control executes the next statement which inserts a new line and returns 0 which specifies that the program is executed without any error.

The output snapshot is provided as follows:

Method 2: Utilizing the Range Constructor to Concatenate the Vectors

The “std::vector” class also provides a constructor that takes two iterators that represent a range and creates a new vector that contains the elements from that range. You can utilize this constructor to concatenate the vectors.

Here’s how you can use the range constructor to concatenate the vectors:

#include <iostream>

#include <vector>

int main()

{

  std::vector<int> p1 = {1, 3, 5};

  std::vector<int> p2 = {7, 9, 11};

  std::vector<int> concatV(p1.begin(), p1.end());

  concatV.insert(concatV.end(), p2.begin(), p2.end());
 
  std::cout << "Concatenated vector:";

  for (int n : concatV) {

    std::cout << n << " ";

  }

  std::cout << std::endl;

  return 0;

}

We create two vectors, “p1” and “p2”. The “p1” vector holds the values 1, 3, and 5. Whereas the “p2” vector has the values 7, 9, and 11. Then, we create a new vector which is “concatV” using the range constructor and pass the beginning and end iterators of the “p1” vector as arguments. This will initialize the “concatV” with the elements of the “p1” vector.

Moreover, the “insert” function is utilized on “concatV” to insert the elements from “p2” at the end of the “concatV” vector. The beginning and end iterators of “p2” are passed as arguments to the insert function. After the insert operation, the “concatV” vector contains the concatenated result of “p1” and “p2”.

A range-based for-loop is used afterward. Every element of the “concatV” vector is iterated over by this loop and is assigned to the “n” variable. Then, it executes the loop body which consists of printing the value of “n” followed by a space that utilizes the “std::cout”.

The concatenated resultant vector can be seen in the following snapshot:

Conclusion

The methods to concatenate two vectors in C++ are discussed in this post. To combine the elements of two vectors into one, there are two approaches that can be implemented. The first method is utilizing the “insert” function by which we can insert the values of one vector into the other. The other method is utilizing the range constructor where we create a new vector and use the insert function, and combine the two vector values into the third vector.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.