mardi 4 juillet 2017

What's the STL way to fill a 2-dimensional std::array without iteration?

I have a 2-dimensional std::array of std::arrays 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::arrays 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