I want to set a default nonzero value for all elements of a table or 2d array. array[size]={12} sets first elements only 12 and others are all 0 in a row.But fill(array,arra
A solution (not really elegant, I know) can be
for ( auto & row : arra )
for ( auto & elem : row )
elem = 45;
or, using std::fill()
for ( auto & row : arra )
std::fill(std::begin(row), std::end(row), 45);
---- EDIT ----
Full example
#include
int main ()
{
int a[10][10];
// mode A
for ( auto & row : a )
for ( auto & elem : row )
elem = 45;
// mode B
for ( auto & row : a )
std::fill(std::begin(row), std::end(row), 47);
for ( int i = 0 ; i < 10 ; ++i )
{
for ( int j = 0 ; j < 10 ; ++j )
std::cout << '[' << a[i][j] << ']';
std::cout << '\n';
}
return 0;
}