Quote:Original post by Zahlman
Yes, but - "the element" is a pointer. The pointer is removed from memory, but the thing it points at is not. This is a very important thing that you should be happy about: something else could be pointing at the same thing, and it would be very bad to take it away!
Unless he intends that the vector have ownership of course.
OP: Would I be correct in assuming that Enemy is a base class, or is it just a non-inherited class? I assume that's why you're storing enemy* instead of Enemy. If its a base, make sure your destructor calls in derived classes are virtual and chain the other destructors properly.
Also, a fast way to delete items from a vector is to partition it such that all the active elements are in the front and the ones to be deleted are in the back -- this is what std::remove_if does. Once it's done its work you then delete the items from the back. This method will result in no extraneous copies of data in the vector (granted, its just a pointer so its not much -- but it can add up for this sort of thing) since delete only occurs at the back. If the contents of the vector don't have to maintain the same order (that is, it doesn't have to be "stable") then you can do even better than remove_if by swapping deleted elements with the last element (don't forget to update the logical "last element" when you do this though) -- basically, get an iterator to head and to tail, walk forward from head and when you find something to remove, swap the contents of head and tail, then step tail forward until head and tail meet.