How to initialize a vector of vectors on a struct? [duplicate]
If I have a NxN matrix
vector< vector<int> > A;
How should I initialize it?
I've tried with no success:
A = new vector(dimension);
neither:
A = new vector(dimension,vector<int>(dimension));
You use new
to perform dynamic allocation. It returns a pointer that points to the dynamically allocated object.
You have no reason to use new
, since A
is an automatic variable. You can simply initialise A
using its constructor:
vector<vector<int> > A(dimension, vector<int>(dimension));
Like this:
#include <vector>
// ...
std::vector<std::vector<int>> A(dimension, std::vector<int>(dimension));
(Pre-C++11 you need to leave whitespace between the angled brackets.)