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

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

      1. <tfoot id='KEJVU'></tfoot>
          <bdo id='KEJVU'></bdo><ul id='KEJVU'></ul>
        <i id='KEJVU'><tr id='KEJVU'><dt id='KEJVU'><q id='KEJVU'><span id='KEJVU'><b id='KEJVU'><form id='KEJVU'><ins id='KEJVU'></ins><ul id='KEJVU'></ul><sub id='KEJVU'></sub></form><legend id='KEJVU'></legend><bdo id='KEJVU'><pre id='KEJVU'><center id='KEJVU'></center></pre></bdo></b><th id='KEJVU'></th></span></q></dt></tr></i><div id='KEJVU'><tfoot id='KEJVU'></tfoot><dl id='KEJVU'><fieldset id='KEJVU'></fieldset></dl></div>
      2. Vector.erase(Iterator) 导致内存访问错误

        时间:2023-09-15

          <small id='3SXkQ'></small><noframes id='3SXkQ'>

                <tbody id='3SXkQ'></tbody>
            1. <legend id='3SXkQ'><style id='3SXkQ'><dir id='3SXkQ'><q id='3SXkQ'></q></dir></style></legend>
                  <bdo id='3SXkQ'></bdo><ul id='3SXkQ'></ul>

                  <i id='3SXkQ'><tr id='3SXkQ'><dt id='3SXkQ'><q id='3SXkQ'><span id='3SXkQ'><b id='3SXkQ'><form id='3SXkQ'><ins id='3SXkQ'></ins><ul id='3SXkQ'></ul><sub id='3SXkQ'></sub></form><legend id='3SXkQ'></legend><bdo id='3SXkQ'><pre id='3SXkQ'><center id='3SXkQ'></center></pre></bdo></b><th id='3SXkQ'></th></span></q></dt></tr></i><div id='3SXkQ'><tfoot id='3SXkQ'></tfoot><dl id='3SXkQ'><fieldset id='3SXkQ'></fieldset></dl></div>
                  <tfoot id='3SXkQ'></tfoot>
                • 本文介绍了Vector.erase(Iterator) 导致内存访问错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试对存储在 vector 中的 videoObjects 进行 Z-Index 重新排序.计划是识别将要放在 vector 的第一个位置的 videoObject,将其擦除,然后将其插入到第一个位置.不幸的是,erase() 函数总是会导致内存访问错误.

                  I am trying to do a Z-Index reordering of videoObjects stored in a vector. The plan is to identify the videoObject which is going to be put on the first position of the vector, erase it and then insert it at the first position. Unfortunately the erase() function always causes bad memory access.

                  这是我的代码:

                  testApp.h:

                  vector<videoObject> videoObjects;
                  vector<videoObject>::iterator itVid;
                  

                  testApp.cpp:

                  testApp.cpp:

                  // Get the videoObject which relates to the user event
                  for(itVid = videoObjects.begin(); itVid != videoObjects.end(); ++itVid) {
                    if(videoObjects.at(itVid - videoObjects.begin()).isInside(ofPoint(tcur.getX(), tcur.getY()))) {
                     videoObjects.erase(itVid);
                    }
                  }
                  

                  这应该很简单,但我只是不明白我在哪里走错了路.

                  This should be so simple but I just don't see where I'm taking the wrong turn.

                  推荐答案

                  你应该做的

                  itVid = videoObjects.erase(itVid);
                  

                  引用自 cplusplus.com:

                  [vector::erase] 使所有迭代器和对 positionfirst 之后元素的引用无效.

                  [vector::erase] invalidates all iterator and references to elements after position or first.

                  返回值:一个随机访问迭代器,指向被函数调用擦除的最后一个元素之后的元素的新位置,如果操作擦除了序列中的最后一个元素,则为向量结束.

                  Return value: A random access iterator pointing to the new location of the element that followed the last element erased by the function call, which is the vector end if the operation erased the last element in the sequence.

                  更新:您访问条件中当前元素的方式看起来很奇怪.还必须避免在 erase 之后增加迭代器,因为这会跳过一个元素并可能导致越界错误.试试这个:

                  Update: the way you access the current element inside your condition looks rather strange. Also one must avoid incrementing the iterator after erase, as this would skip an element and may cause out-of-bounds errors. Try this:

                  for(itVid = videoObjects.begin(); itVid != videoObjects.end(); ){
                    if(itVid->isInside(ofPoint(tcur.getX(), tcur.getY()))){
                      itVid = videoObjects.erase(itVid);
                    } else {
                      ++itVid;
                    }
                  }
                  

                  这篇关于Vector.erase(Iterator) 导致内存访问错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:std::vector 何时重新分配其内存数组? 下一篇:将 std::vector 附加到自身,未定义的行为?

                  相关文章

                  最新文章

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

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

                    1. <legend id='qy0AP'><style id='qy0AP'><dir id='qy0AP'><q id='qy0AP'></q></dir></style></legend>
                    2. <tfoot id='qy0AP'></tfoot>