How to declare the size, and fill vector<vector <T>> ?

Discussion in 'C++' started by eb, Sep 13, 2006.

  1. eb

    eb Guest

    I have this working code :

    foo.h
    /* nothing */

    foo.cpp
    ...
    std::vector<std::vector<T *> > my_T(board_size,board_size) ;

    for (i=0; i<board_size; i++)

    for (j=0; j<board_size; j++)
    {
    my_T[j] = new T ;
    my_T[j]->do_something_with_my_T()
    }
    ....



    I'd like do do the same with :


    foo.h
    std::vector<std::vector<T *> > my_T


    foo.cpp

    ???? -> pass the size to my_T and fill it with T


    Any idea ?
    google did not help me there ...


    (sorry for the inappropriate vocabulary, I'm not a coder in real life).
     
    eb, Sep 13, 2006
    #1
    1. Advertising

  2. eb wrote:
    > I have this working code :
    >
    > foo.h
    > /* nothing */
    >
    > foo.cpp
    > ...
    > std::vector<std::vector<T *> > my_T(board_size,board_size) ;
    >
    > for (i=0; i<board_size; i++)
    >
    > for (j=0; j<board_size; j++)
    > {
    > my_T[j] = new T ;
    > my_T[j]->do_something_with_my_T()
    > }
    > ...
    >
    >
    >
    > I'd like do do the same with :
    >
    >
    > foo.h
    > std::vector<std::vector<T *> > my_T
    >
    >
    > foo.cpp
    >
    > ???? -> pass the size to my_T and fill it with T
    >
    >
    > Any idea ?
    > google did not help me there ...


    Look into 'std::generate'. You can write your own generator that would
    allocate your T and do something with it before assigning it to the
    element of the vector.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 13, 2006
    #2
    1. Advertising

  3. eb

    Earl Purple Guest

    Re: How to declare the size, and fill vector<vector <T>> ?

    eb wrote:
    > I have this working code :
    >
    > foo.h
    > /* nothing */
    >
    > foo.cpp
    > ...
    > std::vector<std::vector<T *> > my_T(board_size,board_size) ;


    You cannot initialise your vector this way. vector does have a
    constructor that takes 2 parameters. The first is the size, the second
    is the element to which each element is initialised. Now as far as I'm
    aware this cosntructor is explicit so there will be no implicit
    conversion from int (or whatever integral type your board_size is) to
    vector< T * >.

    Note that if the constructor is not explicit then it would indeed work.

    > for (i=0; i<board_size; i++)
    >
    > for (j=0; j<board_size; j++)
    > {
    > my_T[j] = new T ;
    > my_T[j]->do_something_with_my_T()
    > }
    > ...


    vector< vector< X > > is not generally the best way to implement a
    matrix. Also beware of having vectors of pointers. And where is this
    initialised? If it's in the constructor of your class you may have
    exception-safety issues as you'll need to clean up the pointers you've
    already allocated if a later one fails. If it isn't in your
    constructor, you'll have a partially initialised vector, i.e. it will
    have some valid pointers but many NULLs and will probably be unusable
    by most of your class functions that will not be expecting a
    partially-initialised vector.

    >
    > I'd like do do the same with :
    >
    >
    > foo.h
    > std::vector<std::vector<T *> > my_T
    >
    >
    > foo.cpp
    >
    > ???? -> pass the size to my_T and fill it with T
    >
    >
    > Any idea ?
    > google did not help me there ...


    Have a look at the matrix example in the comp.lang.c++ FAQ (at
    www.parashift.com) and also consider that you'll want shared_ptr< T >
    as the member type.

    I also have a sophisticated Matrix template based on the one in the FAQ
    which would allow iteration thus you could use a generate() on it to
    fill the values.
     
    Earl Purple, Sep 13, 2006
    #3
    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. Mudcat
    Replies:
    1
    Views:
    305
    Mudcat
    Jan 19, 2005
  2. Replies:
    8
    Views:
    372
    Keith Thompson
    Aug 2, 2006
  3. Replies:
    8
    Views:
    1,985
    Csaba
    Feb 18, 2006
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,698
    Michael Jung
    May 25, 2008
  5. EricFowler
    Replies:
    1
    Views:
    352
    James Kanze
    Mar 29, 2009
Loading...

Share This Page