Confusion on iterators invalidation in deque

后端 未结 3 1980
暗喜
暗喜 2020-12-15 22:38

I\'m bit confused regarding iterator invalidation in deque. (In the context of this question)

Following is the excerpts from -- The C++ Standard Library: A Tutorial

相关标签:
3条回答
  • 2020-12-15 22:51

    The SGI implementation probably uses a growable array, so if an insert causes the array to grow, the iterators pointing to the old array are invalid.

    EDIT:

    Looking in section 17.2.3 of The C++ Programming Language Third Edition, I don't see anything in the description of deque that indicates what operations preserve or invalidate iterators. I may be looking in the wrong spot or the behavior may be undefined.

    0 讨论(0)
  • 2020-12-15 23:08

    IMHO, deque is collection of blocks with first block growing in one direction and the last block in opposite direction.

    Your opinion is your prerogative, but it's wrong. :)

    deque is such a container semantically, but in terms of implementation it's designed to be implemented by one or more blocks of memory. C++'s iterator invalidation rules come from implementation, so this is why. Arguably this is a small abstraction leak but, well, whatever.

    The SGI STL documentation is not the proper documentation to read, because the SGI STL is not the C++ Standard Library. Unfortunately, Josuttis is one of those people who calls it "the STL", and this has led to your confusion.


    Following is the excerpts from -- The C++ Standard Library: A Tutorial and Reference, By Nicolai M. Josuttis

    Any insertion or deletion of elements other than at the beginning or end invalidates all pointers, references, and iterators that refer to elements of the deque.

    Put simply, this passage from Josuttis is misleading in implying that the insertion or deletion of elements that are at the beginning or end do not invalidate pointers, references or iterators … though it's worth noting that he never comes out and asserts this outright.


    Here are the real, proper, official rules for std::deque:

    C++03

    • Insertion: all iterators and references are invalidated, unless the inserted member is at an end (front or back) of the deque (in which case all iterators are invalidated, but references to elements are unaffected) [23.2.1.3/1]

    • Erasure: all iterators and references are invalidated, unless the erased members are at an end (front or back) of the deque (in which case only iterators and references to the erased members are invalidated) [23.2.1.3/4]

    • Resizing: as per insert/erase [23.2.1.2/1]

    C++11

    • Insertion: all iterators and references are invalidated, unless the inserted member is at an end (front or back) of the deque (in which case all iterators are invalidated, but references to elements are unaffected) [23.3.3.4/1]

    • Erasure: erasing the last element invalidates only iterators and references to the erased elements and the past-the-end iterator; erasing the first element invalidates only iterators and references to the erased elements; erasing any other elements invalidates all iterators and references (including the past-the-end iterator) [23.3.3.4/4]

    • Resizing: as per insert/erase [23.3.3.4/1]


    Further reading

    • Iterator invalidation rules
    • What's the difference between "STL" and "C++ Standard Library"?

    I'm not sure what further reference to credible sources you're looking for — the relevant standard passage has already been cited and quoted.

    0 讨论(0)
  • 2020-12-15 23:17

    From the standard working draft

    template < class InputIterator > void insert ( iterator position , InputIterator first , InputIterator last );

    1 Effects: An insert in the middle of the deque invalidates all the iterators and references to elements of the deque. An insert at either end of the deque invalidates all the iterators to the deque, but has no effect on the validity of references to elements of the deque."

    So both are correct. As Josuttis indicates, insertion at the front or back doesn't invalidate references to elements of the deque, only iterators to the deque itself.

    EDIT: A more up-to-date draft says essentially the same thing (section 23.2.2.3)

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