c++ - How to deallocate an element in a vector of pointers? -
so have vector of pointers so:
vector<example*> ve;
i fill vector pointers this
example* e = new example(); ve.push_back(e)
but when want remove them, how assure deallocated? enough?
ve.erase(ve.begin() + 1) delete ve[1]
you have other way round, of course:
delete ve[1]; ve.erase(ve.begin() + 1);
however, it's vastly more preferable use smart pointers (such std::unique_ptr
) instead of raw pointers when expressing ownership.
Comments
Post a Comment