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));
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; // ...
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].
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.
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));
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