Possible problem with iterators in STL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • babak

    #1

    Possible problem with iterators in STL

    Hi everyone
    I have a problem with Iterators and Containers in STL that hopefully
    someone can help me with.
    This is what I try to do:

    I have an associative (map) container and I have a function where I
    with the help of iterators want to search through the container and
    remove a certain object in the container.
    Here is part of the code in that function:

    //Move from timers map container to stale list container
    Timers::iterato r start(_timers.b egin());
    Timers::iterato r last(_timers.en d());


    while (start != last)
    {
    if(compObj.Comp are(start->second))
    {
    _stale.push_bac k(start->second);

    start = _timers.erase(s tart);

    }
    else
    {
    ++start;
    }
    }

    The function works correctly most of the time (maybe 8 out of 10
    times). It then goes through the while loop 3 times. The first time it
    goes into the if case and erases the object and the other 2 times it
    goes to the else case and then it jumps out of the while loop.
    But sometimes it does not behave correctly. In those cases it behaves
    like I described above until the third time in the while loop. The
    function then goes to the else case but never leaves it (i.e it comes
    to the line before ++start but not the line after it when I use
    printfs).

    I believe that all the initializations are made correctly so the only
    thing I can come up with is that something goes wrong with the
    iterator. Is there any possibility that I somehow e.g try to increase
    the iterator out of bounds or make some other error which could lead to
    very strange behaviour? Any other suggestions of what could lead to
    this strange behaviour?

    Thanks for your help.
    Regards.
    /Babak

Working...