madhu wrote:
http://msdn2.microsoft.com/en-us/lib...ce(VS.80).aspx
vector <intv1;
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<Tnewv; //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..