<tfoot id='Brugw'></tfoot>

  1. <legend id='Brugw'><style id='Brugw'><dir id='Brugw'><q id='Brugw'></q></dir></style></legend>
    <i id='Brugw'><tr id='Brugw'><dt id='Brugw'><q id='Brugw'><span id='Brugw'><b id='Brugw'><form id='Brugw'><ins id='Brugw'></ins><ul id='Brugw'></ul><sub id='Brugw'></sub></form><legend id='Brugw'></legend><bdo id='Brugw'><pre id='Brugw'><center id='Brugw'></center></pre></bdo></b><th id='Brugw'></th></span></q></dt></tr></i><div id='Brugw'><tfoot id='Brugw'></tfoot><dl id='Brugw'><fieldset id='Brugw'></fieldset></dl></div>
  2. <small id='Brugw'></small><noframes id='Brugw'>

      <bdo id='Brugw'></bdo><ul id='Brugw'></ul>
    1. STL 迭代器是否保证集合更改后的有效性?

      时间:2024-05-11
    2. <legend id='xnLqr'><style id='xnLqr'><dir id='xnLqr'><q id='xnLqr'></q></dir></style></legend>
        <tbody id='xnLqr'></tbody>

      <small id='xnLqr'></small><noframes id='xnLqr'>

        <bdo id='xnLqr'></bdo><ul id='xnLqr'></ul>

        • <tfoot id='xnLqr'></tfoot>
          1. <i id='xnLqr'><tr id='xnLqr'><dt id='xnLqr'><q id='xnLqr'><span id='xnLqr'><b id='xnLqr'><form id='xnLqr'><ins id='xnLqr'></ins><ul id='xnLqr'></ul><sub id='xnLqr'></sub></form><legend id='xnLqr'></legend><bdo id='xnLqr'><pre id='xnLqr'><center id='xnLqr'></center></pre></bdo></b><th id='xnLqr'></th></span></q></dt></tr></i><div id='xnLqr'><tfoot id='xnLqr'></tfoot><dl id='xnLqr'><fieldset id='xnLqr'></fieldset></dl></div>

                本文介绍了STL 迭代器是否保证集合更改后的有效性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                假设我有某种集合,并且我获得了它开头的迭代器.现在假设我修改了集合.无论集合类型或迭代器如何,我仍然可以安全地使用迭代器吗?

                Let's say I have some kind of collection and I obtained an iterator for the beginning of it. Now let's say I modified the collection. Can I still use the iterator safely, regardless of the type of the collection or the iterator?

                为了避免混淆,以下是我所说的操作顺序:

                To avoid confusion, here is the order of operations I talk about:

                1. 获取集合的迭代器.
                2. 修改集合(显然不是其中的元素,而是集合本身).
                3. 使用在步骤 1 中获得的迭代器.根据 STL 标准它仍然有效吗?!

                推荐答案

                取决于容器.例如如果是vector,修改容器后所有迭代器都可以失效.但是,如果是list,则与修改位置无关的迭代器将保持有效.

                Depends on the container. e.g. if it's a vector, after modifying the container all iterators can be invalidated. However, if it's a list, the iterators irrelevant to the modified place will remain valid.

                • 当重新分配内存时,向量的迭代器失效.此外,在向量中间插入或删除元素会使所有指向插入或删除点之后的元素的迭代器失效.因此,如果您使用 reserve() 预分配向量将使用的尽可能多的内存,并且如果所有插入和删除都在向量的末尾,则可以防止向量的迭代器失效.[1]

                • 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. [1]

                deque 的迭代器失效语义如下.Insert(包括push_frontpush_back)使所有引用deque 的迭代器无效.在 deque 中间的 Erase 使所有引用 deque 的迭代器无效.Erasedeque(包括 pop_frontpop_back)的开头或结尾,仅当它指向时才使迭代器无效到被擦除的元素.[2]

                The semantics of iterator invalidation for deque is as follows. Insert (including push_front and push_back) invalidates all iterators that refer to a deque. Erase in the middle of a deque invalidates all iterators that refer to the deque. Erase at the beginning or end of a deque (including pop_front and pop_back) invalidates an iterator only if it points to the erased element. [2]

                Lists 有一个重要的特性,即插入和拼接不会使列表元素的迭代器失效,即使移除也只会使指向被移除元素的迭代器失效.[3]

                Lists have the important property that insertion and splicing do not invalidate iterators to list elements, and that even removal invalidates only the iterators that point to the elements that are removed. [3]

                Map 具有一个重要的特性,即将新元素插入到 map 中不会使指向现有元素的迭代器失效.从映射中擦除元素也不会使任何迭代器失效,当然,实际上指向被擦除元素的迭代器除外.[4](set 相同,multisetmultimap)

                Map has the important property that inserting a new element into a map does not invalidate iterators that point to existing elements. Erasing an element from a map also does not invalidate any iterators, except, of course, for iterators that actually point to the element that is being erased. [4] (same for set, multiset and multimap)

                这篇关于STL 迭代器是否保证集合更改后的有效性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在迭代时从地图(或任何其他 STL 容器)中擦除/删除内容 下一篇:std::sort() 中使用哪种类型的排序?

                相关文章

                  <bdo id='zZX4r'></bdo><ul id='zZX4r'></ul>

                <i id='zZX4r'><tr id='zZX4r'><dt id='zZX4r'><q id='zZX4r'><span id='zZX4r'><b id='zZX4r'><form id='zZX4r'><ins id='zZX4r'></ins><ul id='zZX4r'></ul><sub id='zZX4r'></sub></form><legend id='zZX4r'></legend><bdo id='zZX4r'><pre id='zZX4r'><center id='zZX4r'></center></pre></bdo></b><th id='zZX4r'></th></span></q></dt></tr></i><div id='zZX4r'><tfoot id='zZX4r'></tfoot><dl id='zZX4r'><fieldset id='zZX4r'></fieldset></dl></div>
                <legend id='zZX4r'><style id='zZX4r'><dir id='zZX4r'><q id='zZX4r'></q></dir></style></legend>
                1. <tfoot id='zZX4r'></tfoot>
                  1. <small id='zZX4r'></small><noframes id='zZX4r'>