std::vector resize bug

There is a certain bug that I keep making again and again.

In C++ there is a standard template library data structure called vector.

It is a data structure that gives you random access to it’s content, much like an array.

A vector can be of variable length and there are several ways to initialize and change it’s size. One of the methods to change it’s size is std::vector::resize.

The advantage of resize is that it can be used after the ctr. But there is a subtle point that needs to be considered.

When you resize std::vector¬†it doesn’t necesseraly call the dtr of it’s content.

The most common issue of this for me is when I want to load new data to std::vector not realizing the old data is still there. Imagine we have std::vector<std::list<unsigned int> >.

After resizing the lists in this case might not be empty.

In order to make sure your vector is empty you should call std::vector::clear first.