Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a vector of vectors on a struct? [duplicate]

Tags:

c++

vector

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)); 
like image 777
anat0lius Avatar asked Feb 09 '14 18:02

anat0lius


People also ask

How do you initialize a vector in a struct?

To properly initialize a structure, you should write a ctor to replace the compiler provided ctor (which generally does nothing). Something like the following (with just a few attributes): struct grupo { float transX, transY; // ...

Can you make a vector of a struct?

To create a vector of structs, define the struct, with a generalized (class) name. Make the template argument of the vector of interest, the generalized name of the struct. Access each cell of the two dimensional structure with the syntax, vtr[i].

How do you initialize a vector to another vector?

Begin Initialize a vector v1 with its elements. Declare another vector v2 and copying elements of first vector to second vector using constructor method and they are deeply copied. Print the elements of v1. Print the elements of v2.


1 Answers

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 image 165
Joseph Mansfield Avatar answered Sep 21 '22 02:09

Joseph Mansfield