Concatenate two vectors in C++
This post will discuss how to join or concatenate two vectors in C++. The resulting vector will contain all the elements of the first vector, followed by all elements of the second vector in the same order.
For example, consider the following vectors x and y, whose concatenation results in vector v.
Input:
x = { 1, 2, 3 };
y = { 4, 5 };
Output:
v = { 1, 2, 3, 4, 5 };
1. Using vector::insert function
The simplest solution is to use a copy constructor to initialize the target vector with the copy of all the first vector elements. Then, call the vector::insert function to copy all elements of the second vector. We can also use only vector::insert to copy elements of both vectors into the destination vector.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream> #include <vector> #include <algorithm> void print(auto const &vector) { for (auto i: vector) { std::cout << i << ' '; } std::cout << std::endl; } int main() { std::vector<int> x = { 1, 2, 3 }; std::vector<int> y = { 4, 5 }; // 1. Copy constructor + vector::insert std::vector<int> v(x); v.insert(v.end(), y.begin(), y.end()); print(v); v.clear(); // clear the vector // 2. only vector::insert v.insert(v.begin(), x.begin(), x.end()); v.insert(v.end(), y.begin(), y.end()); print(v); return 0; } |
2. Using std::copy function
There are many ways to use the std::copy algorithm to concatenate the vectors, as shown below. Please note that std::back_inserter is used to allocate space for the new element in the new vector. Alternately, we can allocate the space beforehand and use normal input iterators.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> #include <vector> #include <algorithm> #include <iterator> void print(auto const &vector) { for (auto i: vector) { std::cout << i << ' '; } std::cout << std::endl; } int main() { std::vector<int> x = { 1, 2, 3 }; std::vector<int> y = { 4, 5 }; // 1. Copy constructor + std::copy + std::back_inserter std::vector<int> v(x); std::copy(y.begin(), y.end(), std::back_inserter(v)); print(v); v.clear(); // clear the vector // 2. std::copy + std::back_inserter std::copy(x.begin(), x.end(), std::back_inserter(v)); std::copy(y.begin(), y.end(), std::back_inserter(v)); print(v); v.clear(); // clear the vector // 3. only std::copy v.resize(x.size() + y.size()); std::copy(x.begin(), x.end(), v.begin()); std::copy(y.begin(), y.end(), v.begin() + x.size()); print(v); return 0; } |
3. Using std::move function
Another efficient solution is to use std::move that actually moves the objects, unlike std::copy, which copies them. We can use it in the same way as std::copy. Please note that the original container elements are left in an unspecified but valid state after std::move is called.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <iostream> #include <vector> #include <algorithm> #include <iterator> void print(auto const &vector) { for (auto i: vector) { std::cout << i << ' '; } std::cout << std::endl; } int main() { std::vector<int> x = { 1, 2, 3 }; std::vector<int> y = { 4, 5 }; // 1. Copy constructor + std::move + std::back_inserter std::vector<int> v(x); std::move(y.begin(), y.end(), std::back_inserter(v)); print(v); v.clear(); // clear the vector // 2. std::move + std::back_inserter std::move(x.begin(), x.end(), std::back_inserter(v)); std::move(y.begin(), y.end(), std::back_inserter(v)); print(v); v.clear(); // clear the vector // 3. only std::move v.resize(x.size() + y.size()); std::move(x.begin(), x.end(), v.begin()); std::move(y.begin(), y.end(), v.begin() + x.size()); print(v); return 0; } |
4. Using std::set_union function
Another approach might be to use std::union that does the union of two sorted ranges. Please note that this might not preserve the original order of elements in both vectors.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <iostream> #include <vector> #include <algorithm> #include <iterator> void print(auto const &vector) { for (auto i: vector) { std::cout << i << ' '; } std::cout << std::endl; } int main() { std::vector<int> x = { 1, 2, 3 }; std::vector<int> y = { 4, 5 }; // 1. std::set_union + std::back_inserter std::vector<int> v; std::set_union(x.begin(), x.end(), y.begin(), y.end(), std::back_inserter(v)); print(v); v.clear(); // 2. std::set_union v.resize(x.size() + y.size()); std::set_union(x.begin(), x.end(), y.begin(), y.end(), v.begin()); print(v); return 0; } |
That’s all about concatenating two vectors in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)