Assigning different sized std::array

arrays, c++, c++11, std

Solution

You could use `std::copy` if the destination array is larger than the source one:

std::array<int, 6> arr1;
std::array<int, 10> arr2;
// Fill arr1...
std::copy(arr1.begin(), arr1.end(), arr2.begin());

If the destination array is shorter, then you'll have to copy up to a certain point. I mean, you can still do this using `std::copy`, but you'll have to do something like:

std::array<int, 10> arr1;
std::array<int, 6> arr2;
// Fill arr1...
std::copy(arr1.data(), arr1.data() + arr2.size(), arr2.begin());

This works for both cases:

std::copy(arr1.data(), arr1.data() + std::min(arr1.size(), arr2.size()), arr2.begin());

Problem

I have a populated std::array with meaningful data and a std::array with 0s. I wish to assign the array of 6 to the array of 8. What is the idiomatic way of doing this in c++?

Original source