Re: Erase in a map

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"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: Using std::equal with an empty set.
    ... I think that the Standard must states explicitly that in case of an empty ... Vladimir Grigoriev ... so you have found an iterator for which the condition isn't true? ...
    (microsoft.public.vc.language)
  • Re: Using std::equal with an empty set.
    ... I believe that when the interval is empty then *otherwise* is true!:) ... Vladimir Grigoriev wrote: ... However if to follow the Standard description it seems that in this case ... It must hold for every iterator in the range [first, ...
    (microsoft.public.vc.language)
  • 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)