From: "Alvin Thompson" <alvin@...>
> so how do you iterate through a list and delete things as you go? if you
> write something as
> map<int,int>::iterator it=map.begin();
> while (it!=map.end())
> 'it' is not guaranteed to remain valid when 'itd' is deleted, and it
> often doesn't when it get to 'end'.
Actually, for a map<>, iterators do remain valid when another element
*besides* the one which the iterator is pointing to is deleted.
from http://www.sgi.com/tech/stl/Map.html :
"Erasing an element from a map also does not invalidate any iterators, except,
of course, for iterators that actually point to the element that is being
Therefore, you can rewrite your loop using a temporary to point to the next
map<int, int>::iterator it(foo.begin());
while (it != foo.end())
if (you want to delete this particular element)
map<int, int>::iterator itTemp(it);
it = itTemp;
Of course, if you wanted to simply delete everything from the map without any
type of conditional processing, you would use map.clear(). Also note that
vector<>, basic_string<>, and rope<> are the containers which normally
allocate memory in one contingous section rather than using a series of
pointers, so this algorithm will not work for those containers.
For general STL and C++ questions like this which aren't directly related to
mingw, I suggest posting in comp.lang.c++.moderated, as you will receive much
more through opinions there.