Concatenate multiple vectors in C++
This post will discuss how to concatenate multiple vectors in C++.
1. Using std::vector::insert
A simple solution to concatenate the contents of a vector into another vector is using the std::vector::insert member function. It can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <vector> int main() { std::vector<int> first = {1, 2, 3}; std::vector<int> second = {4, 5}; first.insert(first.end(), second.begin(), second.end()); for (int &i: first) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5
To concatenate multiple vectors, we can overload the =operator and +=operator for vectors. For better performance, consider invoking the std::vector::reserve function before the std::vector::insert function. Following is a C++ implementation of the same:
|
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> template <typename T> std::vector<T> operator+(std::vector<T> const &x, std::vector<T> const &y) { std::vector<T> vec; vec.reserve(x.size() + y.size()); vec.insert(vec.end(), x.begin(), x.end()); vec.insert(vec.end(), y.begin(), y.end()); return vec; } template <typename T> std::vector<T> &operator+=(std::vector<T> &x, const std::vector<T> &y) { x.reserve(x.size() + y.size()); x.insert(x.end(), y.begin(), y.end()); return x; } int main() { std::vector<int> first = {1, 2, 3}; std::vector<int> second = {4, 5}; std::vector<int> third = {6, 7, 8}; first += second + third; for (int &i: first) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5 6 7 8
2. Using Range v3 library
If permitted, use the ranges-v3 library to lazy concatenate contents of multiple vectors into a new vector. Here’s a one-liner using the ranges::view::concat function, which forms the basis for C++20’s std::ranges.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> #include <range/v3/view.hpp> int main() { std::vector<int> first = {1, 2, 3}; std::vector<int> second = {4, 5}; std::vector<int> third = {6, 7, 8}; std::vector<int> result = ranges::view::concat(first, second, third); for (int &i: result) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5, 6, 7, 8
3. Using std::copy
Alternatively, we can use the std::copy standard algorithm to copy all elements from a given vector to another vector. This is demonstrated below using std::back_inserter, to allocate space for the new element.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> first = {1, 2, 3}; std::vector<int> second = {4, 5}; std::copy(second.begin(), second.end(), std::back_inserter(first)); for (int &i: first) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5
4. Using std::move
Another plausible way to concatenate multiple vectors is using the std::move algorithm. Unlike the std::copy algorithm, std::move actually moves the objects rather than copying them. Its usage remains the same as the std::copy algorithm, as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> first = {1, 2, 3}; std::vector<int> second = {4, 5}; std::move(second.begin(), second.end(), std::back_inserter(first)); for (int &i: first) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5
That’s all about concatenating multiple 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 :)