"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > ## **Do `std::vector::erase`-returned iterators Point to Valid Elements After Removal?**

## **Do `std::vector::erase`-returned iterators Point to Valid Elements After Removal?**

Published on 2024-11-09
Browse:337

## **Do `std::vector::erase`-returned iterators Point to Valid Elements After Removal?**

Invalidation of std::vector Iterators: A Detailed Explanation

The concept of iterator invalidation in std::vector has been frequently discussed. To clarify, the erasure of vector elements via std::vector::erase invalidates iterators positioned strictly after the erased element.

However, the validity of the iterator at the exact position of the erased element remains uncertain. Logically, one might assume this iterator remains valid since the vector's underlying implementation usually shifts remaining elements to fill the empty space. However, the precise behavior and potential for undefined outcomes are less certain.

Consider the following example, which illustrates the removal of odd integers from a vector:

typedef std::vector vectype;
vectype vec;

for (int i = 0; i 

While this code appears to execute without errors in practice, its validity remains debatable.

The answer lies in the behavior of erase: it does indeed invalidate all iterators at or after the iterator(s) passed to erase. However, it also returns a new iterator指向的元素immediately after the erased element(s) or to the end if such an element does not exist. This iterator can be used to resume iteration.

It's important to note that the above method of odd integer removal is inefficient (O(n2)) because each erasure requires a shift of all subsequent elements. The erase-remove idiom offers a far more efficient solution (O(n)):

bool is_odd(int x) { return (x % 2) == 1; }
vec.erase(std::remove_if(vec.begin(), vec.end(), is_odd), vec.end());
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3