I have a 2-dimensional std::array
of std::array
s and I want to fill it with 0s. The only solution to this that I found is to use a pointer to the first element and sizeof
, like so:
std::fill(&arr2d[0][0], &arr2d[0][0] + sizeof(arr2d), 0);
Although it works, it doesn't line up with other methods I've found for filling std::array
s without pointer logic, like the following two:
std::fill(arr1d.begin(), arr1d.end(), 0);
arr1d.fill(0);
I could iterate over each array, like so:
for (auto it : arr2d) {
it.fill(0);
}
but it seems it would be much more efficient to fill the array in one pass. Is there a cleaner method for 2D arrays, or am I stuck with C-style pointer logic?
Aucun commentaire:
Enregistrer un commentaire