I’m trying to delete everything from a std::vector by using the following code
vector.erase( vector.begin(), vector.end() );
but it doesn’t work.
Update: Doesn’t clear destruct the elements held by the vector? I don’t want that, as I’m still using the objects, I just want to empty the container
I think you should use
std::vector::clear:EDIT:
Yes it does. It calls the destructor of every element in the vector before returning the memory. That depends on what “elements” you are storing in the vector. In the following example, I am storing the objects them selves inside the vector:
If you want to share objects between different containers for example, you could store pointers to them. In this case, when
clearis called, only pointers memory is released, the actual objects are not touched:For the question in the comment, I think
getVector()is defined like this:Maybe you want to return a reference: