Initializing a vector of a vector in a constructor

F

fatgirl.brown

Hi all,

I am attempting to initialize a vector of a vector in a constructor
with some clean-looking syntax and am not sure of how to go about this.
For instance:

double weight;
vector< vector<unsigned> > posInd;

Table() :
weight(0), posInd(???)
{}

Table(const TableEntry& o) :
weight(o.weight), posInd(???)
{}

Table(double weight = 0.0, vector< vector<unsigned> > posInd) :
weight(weight), posInd(???)
{}

Many thanks for your patience!
 
N

Nindi73

Hi all,

I am attempting to initialize a vector of a vector in a constructor
with some clean-looking syntax and am not sure of how to go about this.
For instance:

double weight;
vector< vector<unsigned> > posInd;

Table() :
weight(0), posInd(???)
{}

Table(const TableEntry& o) :
weight(o.weight), posInd(???)
{}

Table(double weight = 0.0, vector< vector<unsigned> > posInd) :
weight(weight), posInd(???)
{}

Many thanks for your patience!

The Syntax is the same as if it were just a vector of PODs.

Table() :
weight(0), posInd(5)
{}


would give you a vector of 5 vectors, BUT each of thos 5 vectors would
be default constructed, if you wanted them all to be sized to a size
other than zero, you have no choice but to resize each of them in the
constructor body, for example in a for loop.
N
 
I

Ivan Vecerina

....
: > vector< vector<unsigned> > posInd;
....
: The Syntax is the same as if it were just a vector of PODs.
:
: Table() :
: weight(0), posInd(5)
: {}
:
:
: would give you a vector of 5 vectors, BUT each of thos 5 vectors would
: be default constructed, if you wanted them all to be sized to a size
: other than zero, you have no choice but to resize each of them in the
: constructor body, for example in a for loop.

Actually, if all second-level vectors are to have the same size,
you can define their size at initialization time:
posInd( sizeX, vector<unsigned>( sizeY, 0 ) )

(where 0 could also be replaced by another value to fill
each cell of the 2D vector with).


hth -Ivan
 
N

Nindi73

Ivan said:
...
: > vector< vector<unsigned> > posInd;
...
: The Syntax is the same as if it were just a vector of PODs.
:
: Table() :
: weight(0), posInd(5)
: {}
:
:
: would give you a vector of 5 vectors, BUT each of thos 5 vectors would
: be default constructed, if you wanted them all to be sized to a size
: other than zero, you have no choice but to resize each of them in the
: constructor body, for example in a for loop.

Actually, if all second-level vectors are to have the same size,
you can define their size at initialization time:
posInd( sizeX, vector<unsigned>( sizeY, 0 ) )

(where 0 could also be replaced by another value to fill
each cell of the 2D vector with).

Your absolutley right !!! I am such a pleb !!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top