The destructor should only be called for the element(s) being erased; if you are seeing it called for the others, then your vector
implementation isn't conformant. C++11 23.3.6.5
specifies
Complexity: The destructor of T is called the number of times equal to the number of the elements erased, but the move assignment operator of T is called the number of times equal to the number of elements in the vector after the erased elements.
Older standards specified similar behaviour, but with copy rather than move-assignment.
Your example shows 1
five times because the remaining destructors are called when the vector is destroyed at the end of main
. You can see this more clearly if you output something immediately after the call to erase
.