How to initialize a vector of vectors in a structure?

If I have an NxN matrix

vector< vector<int> > A; 

How to initialize it?

I tried without success:

  A = new vector(dimension); 

neither:

  A = new vector(dimension,vector<int>(dimension)); 
+52
c ++ vector
Feb 09 '14 at 18:41
source share
2 answers

You use new for dynamic allocation. It returns a pointer pointing to a dynamically allocated object.

You have no reason to use new , since A is an automatic variable. You can simply initialize A using your constructor:

 vector<vector<int> > A(dimension, vector<int>(dimension)); 
+109
Feb 09 '14 at 18:47
source share

Like this:

 #include <vector> // ... std::vector<std::vector<int>> A(dimension, std::vector<int>(dimension)); 

(Pre-C ++ 11, you need to leave a space between the angle brackets.)

+14
09 Feb '14 at 18:48
source share



All Articles