Vector of vector question

Discussion in 'C++' started by BCC, Jan 13, 2004.

  1. BCC

    BCC Guest

    If I create a vector of vectors of double:

    std::vector< std::vector<double> > table1;

    Are my vectors of doubles uninitialized? Do I have to loop through table1
    and initialize each vector of doubles using new?

    And in cleaning up, manually delete each of these vectors of doubles?

    Thanks,
    B
     
    BCC, Jan 13, 2004
    #1
    1. Advertising

  2. BCC

    Cy Edmunds Guest

    "BCC" <> wrote in message
    news:gvHMb.9111$...
    > If I create a vector of vectors of double:
    >
    > std::vector< std::vector<double> > table1;
    >
    > Are my vectors of doubles uninitialized?



    No. They are default initialized. You have an empty vector of empty vectors.

    > Do I have to loop through table1
    > and initialize each vector of doubles using new?


    No. Please.

    >
    > And in cleaning up, manually delete each of these vectors of doubles?


    No again. std::vector is a well designed class that doesn't require a lot of
    handholding.

    >
    > Thanks,
    > B
    >
    >
    >




    --
    Cy
    http://home.rochester.rr.com/cyhome/
     
    Cy Edmunds, Jan 13, 2004
    #2
    1. Advertising

  3. In article <gvHMb.9111$>, BCC wrote:
    > If I create a vector of vectors of double:
    >
    > std::vector< std::vector<double> > table1;
    >
    > Are my vectors of doubles uninitialized?


    They're empty. table1.size() will produce 0 and table1[0].size results in
    undefned behaviour (since there is no first element yet)

    > Do I have to loop through table1
    > and initialize each vector of doubles using new?


    You don't use new, the vector class manages its own storage. You use vector
    member functions. If you want the vectors to have some entries, you need to
    do something like this:
    typedef std::vector<double>::size_type dvecsize;
    dvecsize m = 10, n = 5;
    std::vector< std::vector<double> > table1 (m,n);
    std::cout << table1.size() << std::endl; // 10
    std::cout << table1[0].size() << std::endl; // 5

    > And in cleaning up, manually delete each of these vectors of doubles?


    No, the destructor of the vector takes care of deallocating storage. That's
    the main point of having a vector.

    Cheers,
    --
    Donovan Rebbechi
    http://pegasus.rutgers.edu/~elflord/
     
    Donovan Rebbechi, Jan 13, 2004
    #3
  4. BCC

    Daniel T. Guest

    "BCC" <> wrote:

    > If I create a vector of vectors of double:
    >
    > std::vector< std::vector<double> > table1;
    >
    > Are my vectors of doubles uninitialized? Do I have to loop through table1
    > and initialize each vector of doubles using new?
    >
    > And in cleaning up, manually delete each of these vectors of doubles?


    Any doubles created by the vector will be initialized to 0.0, they don't
    need to be 'new'ed nor 'delete'ed.

    Are you sure you want to use a vector of vectors? I would only do that
    if I needed a ragged array. If the array represents a table, you would
    be better off creating a 2D array class. See the FAQ for a sample
    implementation.
     
    Daniel T., Jan 13, 2004
    #4
  5. BCC

    Jon Bell Guest

    In article <gvHMb.9111$>,
    BCC <> wrote:
    >If I create a vector of vectors of double:
    >
    >std::vector< std::vector<double> > table1;
    >
    >Are my vectors of doubles uninitialized?


    In fact, at this point, you have *no* vector<double>s at all. The "outer"
    vector that is supposed to contain vector<double>s has zero size. No
    memory has been allocated at all for storing vector<double>s.

    > Do I have to loop through table1
    >and initialize each vector of doubles using new?


    Assuming you know how big the table is supposed to be (numRows x numCols)
    at run time, before you declare the table, the easiest way is to make the
    table the appropriate size when you declare it:

    std::vector<std::vector<double> > table1 (numRows,
    std::vector<double>(numCols));

    Then fill the table using the usual table1[row][col] notation.

    >And in cleaning up, manually delete each of these vectors of doubles?


    No, std::vector's destructor will take care of any cleanup that is
    necessary, in this case. If you had declared a vector of pointers, then
    you would need to either delete the pointers individually or make sure
    other pointers are pointing to the objects being pointed to, before the
    vector goes out of scope. But you still wouldn't have to worry about
    deleting the vector itself, because you didn't use new to create it.

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Jon Bell, Jan 13, 2004
    #5
  6. BCC

    David Fisher Guest

    "Daniel T." <> wrote:

    > Are you sure you want to use a vector of vectors? I would only do that
    > if I needed a ragged array. If the array represents a table, you would
    > be better off creating a 2D array class. See the FAQ for a sample
    > implementation.


    The reference is
    http://www.parashift.com/c -faq-lite/freestore-mgmt.html#faq-16.17 if you
    didn't already have it.

    David F
     
    David Fisher, Jan 13, 2004
    #6
  7. BCC

    Ron Natalie Guest

    "BCC" <> wrote in message news:gvHMb.9111$...
    > If I create a vector of vectors of double:
    >
    > std::vector< std::vector<double> > table1;
    >
    > Are my vectors of doubles uninitialized? Do I have to loop through table1
    > and initialize each vector of doubles using new?


    There are no elements to initialize, you've created an empty vector of empty
    vectors. However, if you were to give it a size arg (or resize it), then absent
    an explicit value to the constructor or resize call, it will provide default initialized
    values.


    > And in cleaning up, manually delete each of these vectors of doubles?


    No, the vector will take all the elements with them when they go.
     
    Ron Natalie, Jan 13, 2004
    #7
  8. BCC

    David Harmon Guest

    On Tue, 13 Jan 2004 01:37:24 GMT in comp.lang.c++, "Daniel T."
    <> was alleged to have written:
    >Are you sure you want to use a vector of vectors? I would only do that
    >if I needed a ragged array. If the array represents a table, you would
    >be better off creating a 2D array class.


    Well, I can't entirely agree. A vector of vectors is a quick and
    cheerful way of getting a table sized at run time without having to
    reinvent the wheel.

    vector< vector<double> > table1(rows, vector<double>(columns));
     
    David Harmon, Jan 13, 2004
    #8
  9. BCC

    Gavin Deane Guest

    "David Fisher" <> wrote in message news:<jjIMb.7158$>...
    > "Daniel T." <> wrote:
    >
    > > Are you sure you want to use a vector of vectors? I would only do that
    > > if I needed a ragged array. If the array represents a table, you would
    > > be better off creating a 2D array class. See the FAQ for a sample
    > > implementation.

    >
    > The reference is
    > http://www.parashift.com/c -faq-lite/freestore-mgmt.html#faq-16.17 if you
    > didn't already have it.
    >
    > David F


    The next FAQ shows the same thing using a vector of vectors to
    implement the 2D array class.

    http://www.parashift.com/c -faq-lite/freestore-mgmt.html#faq-16.18

    Removes all the need for explicit memory management in the class. And
    it should be easy to design the class so that it's impossible for the
    individual vectors-within-a-vector to end up with different sizes.

    --
    hth
    GJD
     
    Gavin Deane, Jan 13, 2004
    #9
  10. BCC

    Gavin Deane Guest

    David Harmon <> wrote in message news:<>...
    > On Tue, 13 Jan 2004 01:37:24 GMT in comp.lang.c++, "Daniel T."
    > <> was alleged to have written:
    > >Are you sure you want to use a vector of vectors? I would only do that
    > >if I needed a ragged array. If the array represents a table, you would
    > >be better off creating a 2D array class.

    >
    > Well, I can't entirely agree. A vector of vectors is a quick and
    > cheerful way of getting a table sized at run time without having to
    > reinvent the wheel.
    >
    > vector< vector<double> > table1(rows, vector<double>(columns));


    The potential problem is that careless code could end up altering the
    sizes of some of the vector<double>s. Depending on your application,
    you might want the robustness of a class that does not allow this to
    happen.

    --
    GJD
     
    Gavin Deane, Jan 13, 2004
    #10
  11. BCC

    Daniel T. Guest

    (Gavin Deane) wrote:

    > "David Fisher" <> wrote in message
    >
    > > "Daniel T." <> wrote:
    > >
    > > > Are you sure you want to use a vector of vectors? I would only do that
    > > > if I needed a ragged array. If the array represents a table, you would
    > > > be better off creating a 2D array class. See the FAQ for a sample
    > > > implementation.

    > >
    > > The reference is
    > > http://www.parashift.com/c -faq-lite/freestore-mgmt.html#faq-16.17 if you
    > > didn't already have it.
    > >
    > > David F

    >
    > The next FAQ shows the same thing using a vector of vectors to
    > implement the 2D array class.
    >
    > http://www.parashift.com/c -faq-lite/freestore-mgmt.html#faq-16.18
    >
    > Removes all the need for explicit memory management in the class. And
    > it should be easy to design the class so that it's impossible for the
    > individual vectors-within-a-vector to end up with different sizes.


    A better choice, of course would be to implement the 2D array using a
    single vector. This also removes the need for explicit memory management
    and is easer to design...
     
    Daniel T., Jan 14, 2004
    #11
  12. Now, how do we initialize the 2d vector?

    I tried to intialize the 2d vector by doing:(but, it's not working)

    for(int i = 0; i < row; i++)
    for(int j = 0; j < col; j++)
    table1.push_back(1);

    I knew it wouldn't work. I was just trying to do different things.

    I was thinking of using iterator, but I don't have any idea how to do it with 2d vector.
     
    RanggaPratama, Feb 4, 2004
    #12
  13. BCC

    Clark Cox Guest

    In article
    <>,
    "RanggaPratama" <> wrote:

    > Now, how do we initialize the 2d vector?
    >
    > I tried to intialize the 2d vector by doing:(but, it's not working)
    >
    > for(int i = 0; i < row; i++)
    > for(int j = 0; j < col; j++)
    > table1.push_back(1);
    >
    > I knew it wouldn't work. I was just trying to do different things.


    You're not doing anything to change the size of the outer vector.
    Since you said "initialize, I assume that the vector is empty to begin
    with, so table1 is undefined.

    > I was thinking of using iterator, but I don't have any idea how to do it with
    > 2d vector.
    >


    How about (where T is whatever type the inner vector contains):

    {
    table1.clear();
    table1.resize(row, std::vector<T>(col, 1));
    }
     
    Clark Cox, Feb 6, 2004
    #13
    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. pmatos
    Replies:
    6
    Views:
    24,133
  2. David Crawford

    Question on vector of vector iterators

    David Crawford, Dec 15, 2005, in forum: C++
    Replies:
    3
    Views:
    517
  3. Replies:
    8
    Views:
    2,003
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    626
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    399
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page