I have a code where I routinely fill a vector with between 0 and 5000 elements. I know the maximum never exceeds 5000. Instead of initializing vector multiple times, I would like to do just once
vector<struct> myvector;
myvector.reserve(5000);
However, to fill the vector again, I have to clear the vector first without altering its capacity. So usually I call myvector.clear();
This is a O(n) operation. Is there something simple I can do to increase the performance of this or is this about the best that it will get?