for (std::set<Color>::iterator i = myColorContainer.begin(); i!=myColorContainer.end(); ) { if ( *i == Yellow) { DoSomeProccessing( *i ); std::set<Color>::iterator tmp = i; ++i; myColorContainer.erase(tmp); } else { ++i; } }
As soon as you move on to the next c ++i post, it will be valid - the std::set property, so that iterators on the inserted elements are never invalidated, unless the element is deleted.
So now you can safely erase the previous entry.
Artyom
source share