vector tries to maintain performance.
What erase does is it copies elements 2, 3 and 4 to one element, overwriting the element containing 1. So, the first element now contains 2, etc. Then it destroys the last element, which is copy 4.
This erases the first element, but not quite in the way you thought. It erases the contents of the first element, but not the object itself.
Depending on the type that you store in vector , this will be less expensive than destroying the first element, only to restore a copy of the second element to the storage of the first.
Well, if this is an optimization step, then the documentation is incorrect, because:
...
It does not destroy then.
The documentation is correct, and it destroys them.
Your objects are the values ββthat they contain. You stated that your classes were copyable, so vector can copy them. This means that it is absolutely true for two copies to have the same value. vector uses this.
If you made your classes not copied, but intransitive, then they would work correctly. But your move constructor would also have to nullify the moved object, so that none of the two instances would have the same value.
Nicol bolas
source share