Concatenate two arrays together in C++
This post will discuss how to concatenate two arrays together in C++.
1. Using std::copy
The recommended solution is to use the std::copy from the <algorithm> header to concatenate two arrays. The idea is to allocate memory large enough to store all values of both arrays, then copy the values into the new array with std::copy.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; int main() { int first[] = {1, 2, 3}; int second[] = {4, 5}; int m = sizeof(first) / sizeof(*first); int n = sizeof(second) / sizeof(*second); int result[m + n]; std::copy(first, first + m, result); std::copy(second, second + n, result + m); for (int &i: result) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5
The std::copy function returns an iterator to the range’s end where elements have been copied. Therefore, the code can be shortened to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <algorithm> using namespace std; int main() { int first[] = {1, 2, 3}; int second[] = {4, 5}; int m = sizeof(first) / sizeof(*first); int n = sizeof(second) / sizeof(*second); int result[m + n]; std::copy(second, second + n, std::copy(first, first + m, result)); for (int &i: result) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5
Alternately, we can use the std::copy_n standard algorithm, which copies the first n elements of the specified range to the result.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <algorithm> using namespace std; int main() { int first[] = {1, 2, 3}; int second[] = {4, 5}; int m = sizeof(first) / sizeof(*first); int n = sizeof(second) / sizeof(*second); int result[m + n]; std::copy_n(second, n, std::copy_n(first, m, result)); for (int &i: result) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5
2. Using std::memcpy
The std::memcpy performs a binary copy of the arrays of POD (Plain Old Data) type like int, char, etc. We can use it to concatenate two arrays of POD types. It is declared in header <cstring>. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <cstring> using namespace std; int main() { int first[] = {1, 2, 3}; int second[] = {4, 5}; int m = sizeof(first) / sizeof(*first); int n = sizeof(second) / sizeof(*second); int result[m + n]; std::memcpy(result, first, sizeof(first)); std::memcpy(result + m, second, sizeof(second)); for (int &i: result) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5
3. Using Fold Expressions
With C++17, we can use fold expressions to concatenate a sequence of std::arrays. This elegant and efficient solution is demonstrated below:
|
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 |
#include <iostream> #include <array> #include <algorithm> using namespace std; template <typename Type, std::size_t... sizes> auto concatenate(const std::array<Type, sizes>&... arrays) { std::array<Type, (sizes + ...)> result; std::size_t index{}; ((std::copy_n(arrays.begin(), sizes, result.begin() + index), index += sizes), ...); return result; } int main() { std::array<int, 3> first = {{1, 2, 3}}; std::array<int, 2> second = {{4, 5}}; auto result = concatenate(first, second); for (auto &i: result) { std::cout << i << ' '; } return 0; } |
Output:
1 2 3 4 5
That’s all about concatenating two arrays together 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 :)