Re: Erase in a map



"Charles Zhang" <CharlesZhang@xxxxxxxxxxxxxxxxx> wrote in message
news:O24eXUGaHHA.4948@xxxxxxxxxxxxxxxxxxxx
Thank you very much. Your solution works just fine.

Book <<The C++ Standard Library>> written by Nicolai M. Josuttis says
"erase returns nothing". The incorrect statement led to a wrong
direction.

According to the standard, map::erase does return nothing, whereas, say,
vector::erase returns the iterator to the next element after the one
being erased. This is an oversight that will likely be fixed in the next
version of the standard, so that the method signature is uniform across
all containers. The STL implementation shipped with VC has map::erase
returning an iterator as a conforming extension, in anticipation of this
change in the standard.

If you want your code to be standard conforming and portable now,
replace

pos = connectionMap.erase(pos);

with

connectionMap.erase(pos++); // must use post-increment

This works with maps (as well as sets, lists and so on) since map::erase
doesn't invalidate any iterators except those referring to the element
being erased. vector::erase (and deque) on the other hand invalidates
all iterators to the erased element and all following elements, so one
has to use the first version with these containers (luckily, their
erase() method does return an iterator).
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: comparison of objects
    ... the standard function is being used versus your redefinition. ... User's interface would give means to iteration, ... ** User and implementor interface: ... An iterable is an object for which an iterator can be defined. ...
    (comp.lang.lisp)
  • Re: erase
    ... >> that erase returns the next iterator value. ... > signature of erase() becomes consistent across all containers (e.g. ... erase should return an iterator for the associative containers ... in the revised C++ Standard. ...
    (microsoft.public.vc.stl)
  • Re: Erase in a map
    ... erasereturns an iterator of the element after the erased ones. ... According to my pre-release copy of the standard, ... containers that model the sequence requirement. ...
    (microsoft.public.vc.stl)
  • Re: Explain the magic? Counting lines in a file
    ... > you in to the standard name space, what I want to know is what namespace ... Iterator types are special classes that usually belong ... point to a specific element of a container instead of to a specific memory ... Such iterators are used in most standard algorithms, ...
    (comp.lang.cpp)
  • Re: Problem with pointers and iterators
    ... In VC6 this worked because vector::iterator was a typedef for T*. ... Both forms satisfy the requirements of the standard. ... Well, you have an iterator that refers to an object, and you need the address of that object. ... get a reference from the iterator, ...
    (comp.lang.c)