A quick question – possibly of style.
Is it desirable to clear/empty a vector when it’s no longer required, or can you simply rely on STL to clean up after itself when a container is no longer required.
I’m talking in this case about basic vectors that don’t contain pointers or other objects that require delete eg.
std::vector<double> myVector;
// use it
// finished with it
// use clear?
myVector.clear();
There’s no benefit to clearing a vector, since it’s not guaranteed to give up its storage. See
capacity().If you’re really concerned about the memory used by the vector, you can use a little trick to substitute it with an empty vector:
Of course the easiest method is to just let the vector go out of scope and it will be destroyed automatically and all its storage will be freed.