tags:

views:

102

answers:

3

The following code does not work correctly. How should it be done correctly?

for (std::set<Color>::iterator i = myColorContainer.begin();
            i!=myColorContainer.end();
            ++i)
{
    if ( *i  == Yellow)
    {
        DoSomeProccessing( *i );
        myColorContainer.erase(i);
    }
}
+2  A: 

Try:

for(std::set<Color>::iterator it = myColorContainer.begin(); 
    it != myColorContainer.end();) { // note missing it++
     if( (*it) == Yellow ) {
        DoSomeProcessing(*it);
        myColorContainer.erase(it++); // post increment (original sent to erase)
     }
     else {
       ++it; // more efficient than it++;
     }
}
Adrian Regan
This won't work either. You should assign the return value of erase to it again.
Patrick
The returned iterator is a microsoft specific implementation that breaks the standard : http://msdn.microsoft.com/en-us/library/8h4a3515%28VS.80%29.aspx. Sure enough, you need to increment the iterator after the erase.
Adrian Regan
+2  A: 

You don't need a loop as youre dealing with a set.

std::set<Color>::iterator it = myColorContainer.find(Yellow);
if (it != it.myColorContainer.end()){
  DoSomeProcessing(*it);
  myColorContainer.erase(it);
}
Viktor Sehr
As Adrian Regan comments, this is only correct for the microsoft/dinkumware implementation, but still a good solution if you do not need standard adherent, cross platform compability.
daramarak
@Viktor Sehr code is standards compliant. I agree @Viktor Sehr this would be the preferred way to remove an element from the set. However, the question asks how to get the code snippet to work.
Adrian Regan
@daramarak:I think you answered while I edited the code (thought it was a std::vector in my first post)
Viktor Sehr
A: 
for (std::set<Color>::iterator i = myColorContainer.begin();
            i!=myColorContainer.end(); /* No i++ */)
{
    if ( *i  == Yellow)
    {
        DoSomeProccessing( *i );
        std::set<Color>::iterator tmp = i;
        ++i;
        myColorContainer.erase(tmp);
    }
    else {
        ++i;
    }
}

Once you go to next message with ++i it is guaranteed to be valid - property of std::set that iterators on inserted elements are never invalidated unless the element is removed.

So now you can safely erase previous entry.

Artyom