there is
v.erase(v.begin(), v.end());
As fast as
v.clear();
?
I do not need small overheads, such as additional function calls, etc., the compiler will embed this material.
I ask because I have code like the following:
v.erase(v.begin(), last_it);
last_it will usually be the final iterator, but not always. I know that erasing a vector does not have value from the end, because subsequent elements in the vector will need to be copied. In the case where last_it is not the final iterator (rarely), I can handle this. But I do not want to introduce such an overhead when I basically want to clear the vector. So I decided to write my code as follows:
if (last_it == v.end()) { v.clear(); } else { v.erase(v.begin(), last_it); }
I would like to know if this is necessary in order to avoid the penalty for erasing the entire vector. I would prefer my code to be clean and use a single line statement if there is no penalty.
c ++ vector
Neil kirk
source share