Hi I want to initialize a size 9 vector whose elements are vectors of the size, say 5. I want to initialize all the elements to the zero vector.
Is this way correct?
vector<double> z(5,0);
vector< vector<double> > diff(9, z);
OR is there a shorter way to do this?
The default value of a vector is 0. Syntax: // For declaring vector v1(size); // For Vector with default value 0 vector v1(5);
You could potentially do this in a single line:
vector<vector<double> > diff(9, vector<double>(5));
You might also want to consider using boost::multi_array for more efficient storage and access (it avoids double pointer indirection).
You can put it all in one line:
vector<vector<double>> diff(9, vector<double>(5));
This avoids the unused local variable.
(In pre-C++11 compilers you need to leave a space, > >
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With