Should we delete before or after erase. My understanding is both are OK. Is it correct?
In addition, is there any case we won’t want to delete the element while erasing it? I believe there must be , otherwise, the erase will be happy to take the responsibility.
std::vector<foo*> bar;
...
for (vector<foo*>::iterator itr = bar.begin(); itr != bar.end(); itr++)
{
delete (*itr); //before OR
bar.erase(itr);
delete (*itr); //after???
}
“itr” must be used like this;
However, I’d prefer to first delete all elements, and then clear the vector;