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

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

    1. <legend id='lO0lb'><style id='lO0lb'><dir id='lO0lb'><q id='lO0lb'></q></dir></style></legend>

      1. <tfoot id='lO0lb'></tfoot>

        std::vector 迭代器失效

        时间:2023-09-16
      2. <tfoot id='UaWmf'></tfoot>
          <bdo id='UaWmf'></bdo><ul id='UaWmf'></ul>

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

                <tbody id='UaWmf'></tbody>
            • <i id='UaWmf'><tr id='UaWmf'><dt id='UaWmf'><q id='UaWmf'><span id='UaWmf'><b id='UaWmf'><form id='UaWmf'><ins id='UaWmf'></ins><ul id='UaWmf'></ul><sub id='UaWmf'></sub></form><legend id='UaWmf'></legend><bdo id='UaWmf'><pre id='UaWmf'><center id='UaWmf'></center></pre></bdo></b><th id='UaWmf'></th></span></q></dt></tr></i><div id='UaWmf'><tfoot id='UaWmf'></tfoot><dl id='UaWmf'><fieldset id='UaWmf'></fieldset></dl></div>
              <legend id='UaWmf'><style id='UaWmf'><dir id='UaWmf'><q id='UaWmf'></q></dir></style></legend>

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

                  问题描述

                  之前有一些关于这个问题的问题;我的理解是调用 std::vector::erase 只会使位于被擦除元素之后的迭代器无效.但是,擦除元素后,该位置的迭代器是否仍然有效(当然,前提是擦除后它不指向 end())?

                  There have been a few questions regarding this issue before; my understanding is that calling std::vector::erase will only invalidate iterators which are at a position after the erased element. However, after erasing an element, is the iterator at that position still valid (provided, of course, that it doesn't point to end() after the erase)?

                  我对如何实现向量的理解似乎表明迭代器绝对可用,但我不完全确定它是否会导致未定义的行为.

                  My understanding of how a vector would be implemented seems to suggest that the iterator is definitely usable, but I'm not entirely sure if it could lead to undefined behavior.

                  作为我正在谈论的一个例子,以下代码从向量中删除所有奇数.这段代码会导致未定义的行为吗?

                  As an example of what I'm talking about, the following code removes all odd integers from a vector. Does this code cause undefined behavior?

                  typedef std::vector<int> vectype;
                  vectype vec;
                  
                  for (int i = 0; i < 100; ++i) vec.push_back(i);
                  
                  vectype::iterator it = vec.begin();
                  while (it != vec.end()) {
                      if (*it % 2 == 1) vec.erase(it);
                      else ++it;
                  }
                  

                  代码在我的机器上运行良好,但这并不能让我相信它是有效的.

                  The code runs fine on my machine, but that doesn't convince me that it's valid.

                  推荐答案

                  擦除一个元素后,那个位置的迭代器是否仍然有效

                  after erasing an element, is the iterator at that position still valid

                  没有;传递给 erase 的迭代器中或之后的所有迭代器都无效.

                  No; all of the iterators at or after the iterator(s) passed to erase are invalidated.

                  然而,erase 返回一个新的迭代器,该迭代器立即指向被擦除元素之后的元素(如果没有这样的元素,则指向末尾).您可以使用此迭代器来恢复迭代.

                  However, erase returns a new iterator that points to the element immediately after the element(s) that were erased (or to the end if there is no such element). You can use this iterator to resume iteration.

                  请注意,这种移除奇数元素的特殊方法非常低效:每次移除一个元素时,它之后的所有元素都必须向向量左侧移动一个位置(这是 O(n2)).您可以使用 erase-remove idiom (O(n)).您可以创建一个 is_odd 谓词:

                  Note that this particular method of removing odd elements is quite inefficient: each time you remove an element, all of the elements after it have to be moved one position to the left in the vector (this is O(n2)). You can accomplish this task much more efficiently using the erase-remove idiom (O(n)). You can create an is_odd predicate:

                  bool is_odd(int x) { return (x % 2) == 1; }
                  

                  然后这可以传递给remove_if:

                  vec.erase(std::remove_if(vec.begin(), vec.end(), is_odd), vec.end());
                  

                  这篇关于std::vector 迭代器失效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Bjarne Stroustrup 说我们必须避免链表 下一篇:STL 向量和线程安全

                  相关文章

                  最新文章

                1. <legend id='x5rNT'><style id='x5rNT'><dir id='x5rNT'><q id='x5rNT'></q></dir></style></legend>
                2. <small id='x5rNT'></small><noframes id='x5rNT'>

                3. <tfoot id='x5rNT'></tfoot>
                      <bdo id='x5rNT'></bdo><ul id='x5rNT'></ul>

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