vec.clear() clears all elements of the vector, leaving the guarantee vec.size() == 0 .
vec = std::vector<int>() calls the assignment operator copy / move (Since C ++ 11), this replaces the contents of vec with the contents of other . other in this case is the newly constructed empty vector<int> , which means that it has the same effect as vec.clear(); . The only difference is that clear() does not affect the throughput of the vector when reassigned, it resets it.
Old elements are freed properly as with clear() .
Note that vec.clear() always as fast and without an optimizer executing it, most likely faster than creating a new vector and assigning it vec .
Sombrero chicken
source share