Q:

Post increment on set iterator

Possible Duplicate:
iterator validity ,after erase() call in std::set 

When I iterate over a set and want to delete certain items the iterators are changed. This results in segfaults as the iteration fails after deletion. How can I overcome this problem?
std::set< std::pair<double,unsigned>, comparisonFunction> candidates;'
[...]

for( auto it = candidates.begin(); it != candidates.end(); ++it)
{
  [...]
  if ( some constraint satisfied)
  {
    candidates.erase(it);
  }
}

I encounter a segfault when I use this code. My guess is that this is either due to the corrupted iterators or due to the fact, that the element to be deleted is the last element in some cases. Does a post increment on the iterator overcome this problem? Like this:
candidate.erase(it++);

A:

Use the return value of erase:
it = candidates.erase(it);

Note that you must not increment it if you erase an element, otherwise your iterator could be invalidated.
for( auto it = candidates.begin(); it != candidates.end();)
{
  if ( some constraint satisfied)
  {
    it = candidates.erase(it);
  }
  else
    ++it;
}

Also note that this wasn't possible in C++03, since erase didn't return any iterator. However, since you're using C++11 it shouldn't be a problem.
References

std::set::erase

