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
    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!
    , Nov 12, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > 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
    , Nov 12, 2006
    #2
    1. Advertising

  3. <> 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
    #3
  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
    #4
  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
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Moritz Beller
    Replies:
    3
    Views:
    349
  2. jjleto
    Replies:
    2
    Views:
    382
    jjleto
    Nov 12, 2004
  3. pmatos
    Replies:
    6
    Views:
    23,790
  4. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,228
Loading...

Share This Page