Initializing a vector of a vector in a constructor

Discussion in 'C++' started by fatgirl.brown@gmail.com, Nov 12, 2006.

1. Guest

Hi all,

I am attempting to initialize a vector of a vector in a constructor
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(???)
{}

, Nov 12, 2006

2. Guest

wrote:
> Hi all,
>
> I am attempting to initialize a vector of a vector in a constructor
> 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

, Nov 12, 2006

3. Ivan VecerinaGuest

<> wrote in message
news:...
....
: > 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

Ivan Vecerina, Nov 12, 2006
4. Guest

Great - thanks to the both of you!

On Nov 12, 3:57 am, "Ivan Vecerina"
<> wrote:
> <> wrote in messagenews:...
> ...
> : > 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
> --http://ivan.vecerina.com/contact/?subject=NG_POST<- email contact form

, Nov 12, 2006
5. Guest

Ivan Vecerina wrote:
> <> wrote in message
> news:...
> ...
> : > 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 !!!!

, Nov 12, 2006