C++ Code Example: join two arrays

In C++ there are several ways to join two arrays. The first way is to use the copy function of the C++ standard library.

#include <iostream>
using namespace std;

int main() {
    int firstArray[] = {1, 2, 3, 4, 5};
    int secondArray[] = {6, 7, 8, 9, 10};

    int l1 = end(firstArray) - begin(firstArray);
    int l2 = end(secondArray) - begin(secondArray);

    int joinedArray[l1 + l2];
    std::copy(firstArray, firstArray + l1, joinedArray);
    std::copy(secondArray, secondArray + l2, joinedArray + l1);

    for (int i = 0; i < l1 + l2; i++) {
        std::cout << joinedArray[i] << ' ';
    }

    return 0;
}
Output
1 2 3 4 5 6 7 8 9 10