v.erase (pos) and reallocation

I'm studying my teachers notes on STL vectors and she wrote:
"pop_back, erase, clear do not cause reallocation"

I understand that reallocation invalidates all references, pointers and iterator elements of the vector. I also understand how pop_back and clear would not cause reallocation.

However, I don't understand how erase would not cause allocation. When you use v.erase (pos), it removes the element at iterator position pos and returns the position of the next element. How would this NOT cause reallocation?
closed account (2b5z8vqX)
However, I don't understand how erase would not cause allocation. When you use v.erase (pos), it removes the element at iterator position pos and returns the position of the next element. How would this NOT cause reallocation?

I believe you are confusing two different operations. It would be unnecessary to allocate more memory after removing an element.
Think of std::vector as if it has a dynamic array inside in which it stores elements.

Reallocation is when it allocates new memory for that dynamic array.
For instance, push_back() might cause reallocation if the current dynamic array is not large enough to hold the extra element.

However, erasing an element doesn't require the dynamic array to grow in size.
I see, I understand now. Thanks!
It's worth noting that while erase does not cause reallocation, it does cause invalidation of pointers, references and iterators to elements after the one erased.
Topic archived. No new replies allowed.