ITQuants blog

C++: how to clear a STL vector?

Jul 27

Written by:
7/27/2013 11:40 AM  RssIcon

It's an old well-known subject, but since I've seen recently developers who were using the STL vector::clear method and who believed that this method make some deallocation, this post is to remember which method to use in such a case.

In fact, vector::clear() does not make any deallocation. If some push_back method was called, the memory used by the vector after clear remains the same. In order to deallocate at same time, you should better use this code:

vector().swap(m_member_to_clean);

 

where m_member_to_clean is the array to clear.

Tags: C++ , STL , clear , vector
Categories: C++

Search blog