This post will discuss how to join two arrays into a new array in C++. The new array should contain elements of the first array, followed by elements of the second array in the same order.

1. Write our own routine

A naive solution is to create a new array of size enough to accommodate all elements of both arrays and fill it with all elements of the first array, followed by all elements of the second array.

Download  Run Code

Output:

1 2 3 4 5 6

2. Using std::copy function

The recommended approach is to use the std::copy from <algorithm> header, as shown below:

Download  Run Code

Output:

1 2 3 4 5 6

That’s all about joining two arrays in C++.