Merge two Vectors in C++

Merge two vectors in C++

vectors are like dynamic arrays and they can be resized when an element is added or deleted. Their storage is handled automatically by the container. C++ allows multiple features which can help in merging two vectors efficiently. In this article, we will discuss six different ways to merge two vectors in C++.

Using merge() to merge two vectors in C++

In the C++ standard template library, there is a method merge() which can be utilized to combine the elements of two containers in the sorted ranges. The resulting container is sorted in this method and this function is defined in the algorithm header. There are two versions of this method. In the start one < operator is used and a comparator (comp) for the second one for comparing the elements. Here we will use the start method of merge() function to merge two vector containers.

Parameters:
start1 & end1: These are input iterators to the first and last positions of the first sorted container. It contains all the elements in the range [start, end), including the element pointed by the start1 and not the element pointed by the end1.

start2 & end2: These are also input iterators to the first and last positions of the second sorted container.

result: It is an output iterator to store the sorted resulting elements.

Code

Output

First Vector Elements:10 20
Second Vector Elements: 20 30 40 50
New Vector Elements using ‘merge()’: 10 20 20 30 40 50

Using set_union() to merge two vectors in C++

set_union() method of the standard template library can also be used to merge two containers in C++. It can also combine the two containers in the sorted ranges like merge(). But, the resulting container after set_union() will only contain the elements that are present in both the containers only once and in merge(), the resulting container can contain the elements twice (keeping elements from both the ranges).

For example: A={10 , 20, 30, 50, 80} and B = {10, 20, 40};
Output for set_union : C = {10, 20, 30, 40, 50, 80}
Output for merge : D= { 10, 10, 20, 20, 30, 40, 50, 80}

set_union() is also defined in the algorithm header. There are two versions of this method. In the first version a comparison operator is used and a comparator is used for the second one. Here we will use only the first version of this function.

Parameters:
start1 & end1: These are input iterators to the first and last positions of the first sorted container. It contains all the elements in the range [start, end), including the element pointed by the start1 and not the element pointed by the end1.

start2 & end2: These are also input iterators to the first and last positions of the second sorted container.

result: It is an output iterator to store the sorted resulting elements.

Code

Output

//0 in the end gets printed because one element(20) was common in both vectors
//we created vector of size of both vectors
//Iterator can be used in the for loop to print only valid elements in the new vector
First Vector Elements:10 20
Second Vector Elements: 20 30 40 50
New Vector Elements using set_union(): 10 20 30 40 50 0

Using insert() to combine two vectors in C++

vector::insert() is an in-built function in the standard template library of C++. This function can be used to insert elements before the element at a specific position. We can utilize this function to merge two vectors in C++. In this method, we will use a copy constructor to initialize the second vector with the copy of all the elements of the first vector. Then, we will use the insert() function to copy all the elements present in the second vector to the first vector. Elements present in both the vectors can also be directly copied in a new vector using the insert() function.

Parameters:
position: New elements will be added to this position in the vector.
start and end: They specify the input iterator position or range where elements will be inserted. The range includes the element which is pointed by the start variable and not the end variable.

Code

Output

Vector Elements After Merge using Copy Constructor + insert(): 10 20 30 40 50
Vector Elements After Merge using only insert(): 10 20 30 40 50

Using move() & back_inserter to merge two vectors in C++

We can utilize the move() function to move the elements in the range [start, end). This function will add elements in the target container at the position pointed by the output iterator result. We will also use the back_inserter() function in this method to insert new elements at the end of the first vector. It is an output iterator that is made for allowing programs that need to overwrite elements at the end of the container. In this method, we can also merge two vectors using

  1. Copy constructor and both functions (move() & back_inserter())
  2. back_inserter() and move() function only
  3. move() function only

Parameters:
start, end: They specify the input iterator position or range where elements will be inserted. The range includes the element which is pointed by the start variable and not the end variable.
result: It marks the starting position for moving elements in the target container.

Code

Output

Vector Elements After Merge using move Constructor: 10 20 30 40 50
Vector Elements After Merge using move() & back_inserter(): 10 20 30 40 50
Vector Elements After Merge using move() only: 10 20 30 40 50

Using copy() and back_inserter to concatenate two vectors in C++

We can use the copy() in the same way we used the move() function. It can copy the elements in the specified range at the position given in the output iterator. This method will also contain the back_inserter() method as an output iterator. The syntax remains the same as in the previous method, we just have to replace the move with copy. In this method also, we can also merge two vectors using

  1. Copy constructor and both functions (copy() & back_inserter())
  2. back_inserter() and copy() function only
  3. copy() function only

Code

Output

Vector Elements After Merge using move Constructor: 1 2 3 4 5 6 7 8 9 10
Vector Elements After Merge using move() & back_inserter(): 1 2 3 4 5 6 7 8 9 10
Vector Elements After Merge using move() only: 1 2 3 4 5 6 7 8 9 10

Using make_move_iterator() and insert() to merge two vectors in C++

This function make_move_iterator() was introduced first in the C++ 11 version. This function returns move_iterator for the specified iterator iter and the type will be deduced from the argument type.

Note: This method is different from move() & back_inserter because the insert() function might allocate enough memory to store all the elements in a single allocation. But when we use back_inserter() that is equal to multiple push_back() which will do multiple allocations.

Parameters:
iter is the input iterator that can be converted to a move iterator.

Code

Output

New Vector Elements After Merge: 1 2 3 4 5 6 7 8 9 10

Conclusion

We discussed several methods above which are efficient and can be used as per the requirements. It is easy to merge two vectors in C++ using any of the methods discussed above.
Happy Learning!!

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *