If you are using this style and when you erase()
items in vector, be careful dont perform ++iterator
.
Since the pointer to the item that you erase()
, it already release, so the pointer target will become unknown,
and causing undefined behaviour if the iteration keep going.
It is possible cause the vector iteration out-of-bound.
template <class T>
void Container<T>::removeValue(T *value, bool deletePointer)
{
for (iterator i = values.begin(); i != values.end(); ) {
// remove the input value from the container
if ((*i) == value) {
// check whether free the memory allocated to value or not.
if (deletePointer) {
delete valuse;
values.erase(i);
} else {
values.erase(i);
}
}else{
++i; // only update iterator when current iterator is not erased.
}
}
}