erase(it)
invalidates the iterator it
, i.e. it is useless after the erase
and doing anything with it results in undefined behaviour. (You were probably expecting it to 'move to the next element' when the element it's pointing to is erased, but that's not what it does.)
Your second approach doesn't fix this. It may work by chance, but you're still reusing it
after you have erased it.
edit: Given your description "I want to erase only one 5 from multiset and maintain the it valid after erase for next erase.", you can do that by creating a copy of the iterator, incrementing the original and then erasing through the copy:
it = myints.find(5);
// better add a check here to make sure there actually is a 5 ...
std::multiset<int>::iterator newit = it;
it++;
myints.erase(newit);
Since you have already incremented it
, it remains valid because it does not point to the element that is killed by erase
.
However, I honestly cannot imagine a situation in which this might actually be useful, or rather, required.