Does pop_back() really invalidate *all* iterators on an std::vector?

前端 未结 11 1568
半阙折子戏
半阙折子戏 2021-01-05 06:26
std::vector ints;

// ... fill ints with random values

for(std::vector::iterator it = ints.begin(); it != ints.end(); )
{
    if(*it < 10)
         


        
相关标签:
11条回答
  • 2021-01-05 06:40

    Here is your answer, directly from The Holy Standard:

    23.2.4.2 A vector satisfies all of the requirements of a container and of a reversible container (given in two tables in 23.1) and of a sequence, including most of the optional sequence requirements (23.1.1).
    23.1.1.12 Table 68 expressiona.pop_back() return typevoid operational semanticsa.erase(--a.end()) containervector, list, deque

    Notice that a.pop_back is equivalent to a.erase(--a.end()). Looking at vector's specifics on erase:

    23.2.4.3.3 - iterator erase(iterator position) - effects - Invalidates all the iterators and references after the point of the erase

    Therefore, once you call pop_back, any iterators to the previously final element (which now no longer exists) are invalidated.

    Looking at your code, the problem is that when you remove the final element and the list becomes empty, you still increment it and walk off the end of the list.

    0 讨论(0)
  • 2021-01-05 06:41

    Error is that when "it" points to the last element of vector and if this element is less than 10, this last element is removed. And now "it" points to ints.end(), next "it++" moves pointer to ints.end()+1, so now "it" running away from ints.end(), and you got infinite loop scanning all your memory :).

    0 讨论(0)
  • 2021-01-05 06:47

    The "official specification" is the C++ Standard. If you don't have access to a copy of C++03, you can get the latest draft of C++0x from the Committee's website: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2723.pdf

    The "Operational Semantics" section of container requirements specifies that pop_back() is equivalent to { iterator i = end(); --i; erase(i); }. the [vector.modifiers] section for erase says "Effects: Invalidates iterators and references at or after the point of the erase."

    If you want the intuition argument, pop_back is no-fail (since destruction of value_types in standard containers are not allowed to throw exceptions), so it cannot do any copy or allocation (since they can throw), which means that you can guess that the iterator to the erased element and the end iterator are invalidated, but the remainder are not.

    0 讨论(0)
  • 2021-01-05 06:51

    Iterators are only invalidated on reallocation of storage. Google is your friend: see footnote 5.

    Your code is not working for other reasons.

    0 讨论(0)
  • 2021-01-05 06:52

    Here is a quote from SGI's STL documentation (http://www.sgi.com/tech/stl/Vector.html):

    [5] A vector's iterators are invalidated when its memory is reallocated. Additionally, inserting or deleting an element in the middle of a vector invalidates all iterators that point to elements following the insertion or deletion point. It follows that you can prevent a vector's iterators from being invalidated if you use reserve() to preallocate as much memory as the vector will ever use, and if all insertions and deletions are at the vector's end.

    I think it follows that pop_back only invalidates the iterator pointing at the last element and the end() iterator. We really need to see the data for which the code fails, as well as the manner in which it fails to decide what's going on. As far as I can tell, the code should work - the usual problem in such code is that removal of element and ++ on iterator happen in the same iteration, the way @mikhaild points out. However, in this code it's not the case: it++ does not happen when pop_back is called.

    Something bad may still happen when it is pointing to the last element, and the last element is less than 10. We're now comparing an invalidated it and end(). It may still work, but no guarantees can be made.

    0 讨论(0)
  • 2021-01-05 06:52

    You might want to consider using the return value of erase instead of swapping the back element to the deleted position an popping back. For sequences erase returns an iterator pointing the the element one beyond the element being deleted. Note that this method may cause more copying than your original algorithm.

    for(std::vector<int>::iterator it = ints.begin(); it != ints.end(); )
    {
        if(*it < 10)
            it = ints.erase( it );
        else
            ++it;
    }
    

    std::remove_if could also be an alternative solution.

    struct LessThanTen { bool operator()( int n ) { return n < 10; } };
    
    ints.erase( std::remove_if( ints.begin(), ints.end(), LessThanTen() ), ints.end() );
    

    std::remove_if is (like my first algorithm) stable, so it may not be the most efficient way of doing this, but it is succinct.

    0 讨论(0)
提交回复
热议问题