How to initialize all elements of a two-dimensional array to a particular value?
You can use std::fill
:
for(auto &arr : two_dim)
std::fill(std::begin(arr), std::end(arr), value);
This will work for many arrays and containers, like std::vector
, std::array
, and C arrays.
Also note that you can use memset
to initialize all elements of an array to values other than -1
and 0
. It's just that all the bytes in each element will have the same value, like 0x12121212
.