Suppose I have an ivar vector<type> myVector;
.
I add items to this vector.
Later, I want to "reset" the vector to nothing and add items to again fresh.
Can anyone confirm that the following are true:
myVector.clear(); //removes objects but does not remove memory occupied by prior full size of the vector, even though the vector is now empty with a size of 0
Note that these two sites completely contradict each other: This one says the past-the-end iterators are not invalidated with a clear
while this one instead indicates that all iterators, pointers and references related to this container are invalidated. Either way, since the capacity
is not changed, then this does not really "reset" the vector
.
void someFunction(){
vector<type> emptyVector;
myVector.swap(emptyVector);
} // function terminates, thus the prior contents of myVector are deleted when emptyVector goes out of scope
This seems like the best approach and I'd think it achieves the same thing as:
myVector.swap(vector<type>());
Now how is this any better or worse than simply doing this:
myVector=vector<type>();
This will simply set the whole shebang to a new empty vector, so the old spot in memory will automatically be wiped cleaned, right?