Is the following code guaranteed to work? I have a hashed set of pointers,
which I would like to selectively delete from. Can I iterate through the
set, deleting certain ones, without invalidating the iterator? This
ominous statement in the STL "set" documentation makes me worried. I saw
no similar statement on the "hash_multiset" docs.
"Erasing an element from a set also does not invalidate any iterators,
except, of course, for iterators that actually point to the element that is
being erased."
hash_multiset<mytype*> myset;
// insert a bunch of mytype pointers into myset
for (hash_multiset<mytype*>::iterator i=myset.begin(); i!=myset.end(); i++)
if (mytype->someproperty() == 1) {
mytype* p = *i;
myset.erase(i);
delete p;
}
Thanks.