dimanche 4 novembre 2018

delete an object both from vector and memory whle iterating

I trying to to delete an object both from a vector of objects and from memory using its destructor. I understood the deleting the the object that the iterator points to, is making the the iterator to point to the element that follows the last element removed. Therefore, I tried to implement this:

std::vector<Customer*>::iterator j=customersList.begin();
while (j!=customersList.end()){
    customersList.erase(j);
    delete  *j;
}

is it o.k. or that it jumps 2 places by applying both erase and delete?

Aucun commentaire:

Enregistrer un commentaire