Re: Erase in a map



for ( pos = connectionMap.begin(); pos != connectionMap.end(); ){
ConnectionInfo* conn = pos->second;

if ( current - conn->lastRequestTime > 60 * timeOut ) {
pos = connectionMap.erase(pos);
}
else
++pos;

}


"Charles Zhang" <CharlesZhang@xxxxxxxxxxxxxxxxx> wrote in message news:%23Rp6mF$ZHHA.3272@xxxxxxxxxxxxxxxxxxxxxxx
I want to erase items in a map based on some criteria. However, as soon
as the erase is called, iterator become invalid. I would like someone
to tell me the better way to do it.

Here is the source code I am using, and it is crashing.

for ( pos = connectionMap.begin(); pos != connectionMap.end(); ++pos){
ConnectionInfo* conn = pos->second;

if ( current - conn->lastRequestTime > 60 * timeOut ) {
connectionMap.erase(pos);
}
}


To overcome the problem, I start the loop from the beginning after a
item is removed. But this is too slow.

Thanks

Charles Zhang


Relevant Pages

  • Re: Erase in a map
    ... Charles Zhang wrote: ... as the erase is called, iterator become invalid. ...
    (microsoft.public.vc.stl)
  • Re: remove an item in a STL list
    ... the sequence becomes invalid. ... The iterator returned by erase() points to ... the returned iterator is invalid. ...
    (comp.lang.cpp)
  • Re: how can I loop a std::list ?
    ... > Siemel Naran wrote: ... the iterator is invalid after the erase. ...
    (comp.lang.cpp)
  • Re: Erase in a map
    ... as the erase is called, iterator become invalid. ... const long current; ...
    (microsoft.public.vc.stl)
  • Re: clear() on vectors
    ... >> i am trying to write a vector class that includes a erase and clear ... >>without copying and ammending the header file. ... >> clearcalled the 2 iterator erase function, ...
    (alt.comp.lang.learn.c-cpp)

Loading