STL Vector - clear() works for 2D Vectors?

Discussion in 'C++' started by madhu, Nov 10, 2006.

  1. madhu

    madhu Guest

    http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx

    vector <int> v1;
    v1.push_back( 10 ); //adds 10 to the tail
    v1.push_back( 20 ); //adds 20 to the tail
    cout << "The size of v1 is " << v1.size( ) << endl;
    v1.clear( ); //clears the vector

    I have a few questions:

    Does clear() deallocates the memory too (like resize())?
    Does clear() work for 2D vectors?
    Or clear() is to be called for each dimension?

    thanks in advance..
     
    madhu, Nov 10, 2006
    #1
    1. Advertising

  2. madhu

    ralph Guest

    madhu wrote:

    > http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx
    >
    > vector <int> v1;
    > v1.push_back( 10 ); //adds 10 to the tail
    > v1.push_back( 20 ); //adds 20 to the tail
    > cout << "The size of v1 is " << v1.size( ) << endl;
    > v1.clear( ); //clears the vector
    >
    > I have a few questions:
    >
    > Does clear() deallocates the memory too (like resize())?


    This is implementation defined. Same goes for resize().

    > Does clear() work for 2D vectors?
    > Or clear() is to be called for each dimension?


    What do you mean by 2D vectors?

    Ralpe
     
    ralph, Nov 10, 2006
    #2
    1. Advertising

  3. madhu

    werasm Guest

    madhu wrote:
    > http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx
    >
    > vector <int> v1;
    > v1.push_back( 10 ); //adds 10 to the tail
    > v1.push_back( 20 ); //adds 20 to the tail
    > cout << "The size of v1 is " << v1.size( ) << endl;
    > v1.clear( ); //clears the vector
    >
    > I have a few questions:
    >
    > Does clear() deallocates the memory too (like resize())?


    As far as I'm aware of, both clear() and resize() does not perform any
    de-allocation in terms of the memory allocated for the items. It only
    calls the destructors of the items that were erased. A vector's
    capacity (which is relative to the amount of contigious memory that it
    represents) grows with amortized constant time as new items are added.
    It never shrinks, unless you do this:

    std::vector<T> newv; //empty
    oldv.swap( newv );

    As far as 2D vectors are concerned, clear will erase all the items in
    the first (or outer) dimension vector. This will cause destructors of
    all items to be called, which effectively deletes all the unerlying
    vectors - which of course erases the items that they contained, so YES.

    R(r)esize will compare the current size, and erase items if excessive
    items exist. If to little items exist, it may perform re-allocation,
    causing all existing iterators to become invalid. This (invalidated
    iterators) will obviously be the case for clear too.

    Regards,

    Werner



    > Does clear() work for 2D vectors?
    > Or clear() is to be called for each dimension?
    >
    > thanks in advance..
     
    werasm, Nov 10, 2006
    #3
  4. madhu

    Mark P Guest

    madhu wrote:
    > http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx
    >
    > vector <int> v1;
    > v1.push_back( 10 ); //adds 10 to the tail
    > v1.push_back( 20 ); //adds 20 to the tail
    > cout << "The size of v1 is " << v1.size( ) << endl;
    > v1.clear( ); //clears the vector
    >
    > I have a few questions:
    >
    > Does clear() deallocates the memory too (like resize())?


    clear() doesn't necessary deallocate memory-- you don't need to worry
    about this as it's the vector destructor's job to make sure that any
    allocated memory is eventually freed. It does, however, invoke the
    destructor of any object that gets cleared out of the vector.

    > Does clear() work for 2D vectors?


    There's no such thing as a 2D vector (except in Physics class). What I
    assume you're asking about is a vector of vectors and in this case, yes,
    calling clear() does what you would expect it to: it invokes the
    destructor of each of its contained vectors and, in the course of its
    destruction, each of these vectors does the same for all of its
    contained objects.
     
    Mark P, Nov 10, 2006
    #4
  5. madhu

    Guest

    Mark P wrote:
    > What I
    > assume you're asking about is a vector of vectors and in this case, yes,
    > calling clear() does what you would expect it to: it invokes the
    > destructor of each of its contained vectors and, in the course of its
    > destruction, each of these vectors does the same for all of its
    > contained objects.



    That's interesting..
    But does this mean that a single call would remove all the objects in
    all the dimenstions?
    Or it is to be done iteratively for each dimention?

    I well could have a vector of a vector of a vector (a.k.a. 3D).. or
    maybe even higher.

    - Divya Rathore
    (remove underscores for email ID)
     
    , Nov 10, 2006
    #5
  6. madhu

    Mark P Guest

    wrote:
    > Mark P wrote:
    >> What I
    >> assume you're asking about is a vector of vectors and in this case, yes,
    >> calling clear() does what you would expect it to: it invokes the
    >> destructor of each of its contained vectors and, in the course of its
    >> destruction, each of these vectors does the same for all of its
    >> contained objects.

    >
    >
    > That's interesting..
    > But does this mean that a single call would remove all the objects in
    > all the dimenstions?
    > Or it is to be done iteratively for each dimention?
    >
    > I well could have a vector of a vector of a vector (a.k.a. 3D).. or
    > maybe even higher.
    >


    Only one call is needed to start the recursive process.

    Clearing or destructing a vector will destruct all of its contents. If
    those contents are vectors then destructing any of those vectors will
    destruct all of its contents. If those contents are vectors then
    destructing any of those vectors will destruct all of its contents. If
    those contents are vectors... get the idea?
     
    Mark P, Nov 10, 2006
    #6
    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. Creighton Hogg

    vector of vector of vectors

    Creighton Hogg, Jan 20, 2006, in forum: C++
    Replies:
    4
    Views:
    346
    Daniel T.
    Jan 21, 2006
  2. Replies:
    8
    Views:
    1,967
    Csaba
    Feb 18, 2006
  3. Jess
    Replies:
    9
    Views:
    421
  4. Steve
    Replies:
    2
    Views:
    517
    Andre Kostur
    Nov 6, 2007
  5. Guest
    Replies:
    0
    Views:
    461
    Guest
    Sep 14, 2005
Loading...

Share This Page