In C++03, clear()
on a vector is defined in terms of erase(begin(),end())
, exactly the same as resize(0)
, which implies that the capacity stays untouched.
While in C++11, clear()
is not defined in this way. Instead, it says the following:
Destroys all elements in a. Invalidates all references, pointers, and iterators referring to the elements of a and may invalidate the past-the-end iterator.
Does it mean in C++11 an implementation is allowed to deallocate and change the capacity of a vector upon a call to clear()
? Can we expect v.clear()
to have the same effect as vector<T>{}.swap(v)
?
PS: Another question of mine also related to vector storage: What happens to the underlying storage upon vector's copy/move assignment?
Any input is highly appreciated on that one too.