As a function argument I get a
vec.resize( n, 0.0 );
vec.resize( n );
vec.assign( n, 0.0 );
After this, vec is guaranteed to have size and capacity n, with all values 0.0.
Perhaps the more idiomatic way since C++11 is
vec.assign(n, 0.); vec.shrink_to_fit();
with the second line optional. In the case where
vec starts off with more than
n elements, whether to call
shrink_to_fit is a trade-off between holding onto more memory than is required vs performing a re-allocation.