Search code examples
c++vectorerase

Will the end address of the vector change if we erase some elements from the vector because in vector, memory is allotted sequentially (same as array)


If I erase vector elements using the erase() function in c++, will the address of the end element also shift since the memory in vector is allotted sequentially?


Solution

  • Yes you are correct.

    More formally, all iterators and references at or after the first point of the erase, including the end() iterator, are invalidated.

    In other words the addresses of the elements prior to the point of the erase stay the same, but not those after.