迭代器无效规则 [英] Iterator invalidation rules

查看:153
本文介绍了迭代器无效规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++容器的迭代器无效规则是什么?



最好是以汇总列表格式。




(注意:这是一个条目,用于 Stack Overflow的C ++常见问题。如果您想批评在此表单中提供常见问题解答的概念,则在发起所有这一切的meta上的发布将是这样做的地方。这个问题的答案在 C ++聊天室,其中常见问题的想法从第一位开始,所以您的答案很可能会由想出想法的人阅读。)

解决方案

C ++ 11 (来源:迭代器无效化规则(C ++ 0x)



< hr>

插入



序列容器




  • 向量:插入点之前的所有迭代器和引用不受影响,除非新容器大小大于先前容量所有迭代器和引用都无效)[23.3.6.5/1]

  • deque 插入的成员位于deque的结尾(前面或后面)(在这种情况下,所有迭代器都无效,但对元素的引用不受影响)[23.3.3.4/1]

  • code> list :所有迭代器和引用未受影响[23.3.5.4/1]

  • forward_list :所有迭代器和引用不受影响(适用于 insert_after [23.3.4.5/1]

  • array (n / a)



< 关联容器




  • [multi] {set,map} :所有迭代器和引用未受影响[23.2.4 / 9]



p>


  • unordered_ [multi] {set,map} :所有迭代器在重新散列时失效,但引用不受影响[23.2.5 / 8]。如果插入不会导致容器的大小超过 z * B ,则不会发生重新散列,其中 z 是最大负载因子和 B 当前的桶数。 [23.2.5 / 14]



容器适配器




  • stack :继承底层容器

  • code>:继承底层容器


  • priority_queue ul>




    擦除



    / p>


    • 向量:擦除点或其后的每个迭代器和引用无效[ 23.3.6.5/3]

    • deque :擦除最后一个元素只会使迭代器和引用删除元素和过去-end迭代器;擦除第一元素仅使迭代器和对被擦除元素的引用无效;擦除任何其他元素使所有迭代器和引用(包括过去的迭代器)无效[23.3.3.4/4]

    • list :只有迭代器和对已擦除元素的引用无效[23.3.5.4/3]

    • forward_list :只有迭代器和对已擦除元素的引用无效(适用于 erase_after [23.3.4.5/1]

    • array (n / a)



    < 关联容器




    • [multi] {set,map} :只有迭代器和对已删除元素的引用无效[23.2.4 / 9]



    / em>




    • unordered_ [multi] {set,map} 并且对已删除元素的引用无效[23.2.5 / 13]



    >


    • stack :继承底层容器

    • code>:
    • $ :继承底层容器 li>





    调整大小




    • 矢量:按插入/删除[23.3.6.5/12]

    • :as per insert / erase [23.3.3.3/3]

    • erase [23.3.5.3/1]

    • forward_list :根据插入/擦除[23.3.4.5/25]
    • 数组:(n / a)



    <

    注1 > (
    $ b b显式地或通过在其他函数方面定义函数
    ),调用
    a容器成员函数或将
    a容器作为参数传递给 b $ b库函数不会无效
    迭代器更改或更改该容器中的
    对象的值。
    [23.2.1 / 11]




    注2



    < >

    没有swap()函数使任何
    引用,指针或迭代器
    无效
    指向要交换的
    容器的元素。 [注意:
    end()迭代器
    不会引用任何
    元素,因此可能会失效
    -end note] [23.2.1 / 10]




    注3



    除了上述关于 swap()不清楚end迭代器是否受上面列出的每个容器规则的约束;



    注4



    向量和所有无序关联容器支持 reserve(n),这保证不会发生自动调整大小,容器增长到 n 。请注意应使用无序关联容器 ,因为未来的提案将允许指定最小负载因子,这将允许在 insert 足够的擦除操作将容器大小减小到最小值以下;在擦除后,该保证应视为可能无效。


    What are the iterator invalidation rules for C++ containers?

    Preferably in a summary list format.

    (Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom, where the FAQ idea started out in the first place, so your answer is very likely to get read by those who came up with the idea.)

    解决方案

    C++11 (Source: Iterator Invalidation Rules (C++0x))


    Insertion

    Sequence containers

    • vector: all iterators and references before the point of insertion are unaffected, unless the new container size is greater than the previous capacity (in which case all iterators and references are invalidated) [23.3.6.5/1]
    • deque: 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]
    • list: all iterators and references unaffected [23.3.5.4/1]
    • forward_list: all iterators and references unaffected (applies to insert_after) [23.3.4.5/1]
    • array: (n/a)

    Associative containers

    • [multi]{set,map}: all iterators and references unaffected [23.2.4/9]

    Unsorted associative containers

    • unordered_[multi]{set,map}: all iterators invalidated when rehashing occurs, but references unaffected [23.2.5/8]. Rehashing does not occur if the insertion does not cause the container's size to exceed z * B where z is the maximum load factor and B the current number of buckets. [23.2.5/14]

    Container adaptors

    • stack: inherited from underlying container
    • queue: inherited from underlying container
    • priority_queue: inherited from underlying container

    Erasure

    Sequence containers

    • vector: every iterator and reference at or after the point of erase is invalidated [23.3.6.5/3]
    • deque: 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]
    • list: only the iterators and references to the erased element is invalidated [23.3.5.4/3]
    • forward_list: only the iterators and references to the erased element is invalidated (applies to erase_after) [23.3.4.5/1]
    • array: (n/a)

    Associative containers

    • [multi]{set,map}: only iterators and references to the erased elements are invalidated [23.2.4/9]

    Unordered associative containers

    • unordered_[multi]{set,map}: only iterators and references to the erased elements are invalidated [23.2.5/13]

    Container adaptors

    • stack: inherited from underlying container
    • queue: inherited from underlying container
    • priority_queue: inherited from underlying container

    Resizing

    • vector: as per insert/erase [23.3.6.5/12]
    • deque: as per insert/erase [23.3.3.3/3]
    • list: as per insert/erase [23.3.5.3/1]
    • forward_list: as per insert/erase [23.3.4.5/25]
    • array: (n/a)

    Note 1

    Unless otherwise specified (either explicitly or by defining a function in terms of other functions), invoking a container member function or passing a container as an argument to a library function shall not invalidate iterators to, or change the values of, objects within that container. [23.2.1/11]

    Note 2

    no swap() function invalidates any references, pointers, or iterators referring to the elements of the containers being swapped. [ Note: The end() iterator does not refer to any element, so it may be invalidated. —end note ] [23.2.1/10]

    Note 3

    Other than the above caveat regarding swap(), it's not clear whether "end" iterators are subject to the above listed per-container rules; you should assume, anyway, that they are.

    Note 4

    vector and all unordered associative containers support reserve(n) which guarantees that no automatic resizing will occur at least until the size of the container grows to n. Caution should be taken with unordered associative containers because a future proposal will allow the specification of a minimum load factor, which would allow rehashing to occur on insert after enough erase operations reduce the container size below the minimum; the guarantee should be considered potentially void after an erase.

    这篇关于迭代器无效规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆