bool vizitat[60][60];
...
if(/*some condition*/)
{
vizitat[n][m] = {0}; // set all the elements to `false`.
/*other code here*/
}
Is there a way of setting every element of vizitat
to 0, ideally without an explicit loop?
bool vizitat[60][60];
...
if(/*some condition*/)
{
vizitat[n][m] = {0}; // set all the elements to `false`.
/*other code here*/
}
Is there a way of setting every element of vizitat
to 0, ideally without an explicit loop?
Since vizitat
is declared as bool vizitat[60][60];
,
std::fill(
&vizitat[0][0]/*the first element*/,
&vizitat[0][0] + sizeof visitat / sizeof(bool) /*one after the last element*/,
false
);
would do it.
This is because the data in the array are contiguous. Using std::memset
is not safe in general although a compiler might optimise to that.
Note that this approach would not work if the memory was allocated, say, row-by-row. For more details see How are multi-dimensional arrays formatted in memory?