Re: [C++] Vectors & Dynamic allocation

From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 04/30/04


Date: Fri, 30 Apr 2004 10:35:48 +0100

In message <bm44909io91ceu7kt4kjhforg3nmvjp8m8@4ax.com>, Robert W Hand
<rwhand@NOSPAMoperamail.com> writes
>In the section of the Standard devoted to std::vector<T>::erase, there
>is a paragraph labeled "Complexity". It states that the destructor of
>T is called the number of times equal to the number of elements
>erased,

Indeed it says that, but I do not know why because erase() for vectors
does not remove elements, you have to use std::remove() to do that. All
erase does is to write new values into the erased elements (using copy
assignment).

> but the assignment operator for T is called the number of
>times equal to the number of elements after the erased elements.
>There is no provision for deallocation. Furthermore, since iterators
>before the erasure remain valid, an allocation/deallocation step seems
>impossible. Deallocation/allocation would invalidate all the
>iterators.

And I do not know why it mentions invalidation of iterators as it seems
to me that the iterators remain valid but the values of the objects they
identify changes.

-- 
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


Relevant Pages

  • Re: Effective STL Item 4 (size() vs. empty())
    ... I intentionally ignored the unequal allocators case. ... splice is a closely related issue. ... to be able to keep iterators to specific items. ... > container as an argument to a library function shall not invalidate iterators ...
    (comp.lang.cpp)
  • Re: using copy with list<string> and list<cstring*>
    ... Pointers and iterators into the vector are easily ... > Indexes invalidate at exactly the same time that all iterators ... You can adjust them to bring them back into order. ...
    (microsoft.public.vc.stl)
  • Re: howto get object-pointers from iterators?
    ... >> references and iterators, so unless you can guarantee that sequence ... containers are also allowed to invalidate iterators in other ... Which circumstances is explicitly described for each container in the ...
    (alt.comp.lang.learn.c-cpp)
  • Re: vector.erase() ?
    ... if they are reverse iterators. ... because the erase would invalidate the new ...
    (comp.lang.cpp)
  • Re: remove an item in a STL list
    ... Erasing an element from a vector is permitted to invalidate ... >>all iterators referring to elements of the vector. ... > pointing to or after the point of the erase. ... iterators pointing to or after the erased element become invalid. ...
    (comp.lang.cpp)