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

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

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

      如何擦除 &amp;删除指向存储在向量中的对象的

      时间:2023-09-15
      <tfoot id='XaZ2w'></tfoot>
    2. <i id='XaZ2w'><tr id='XaZ2w'><dt id='XaZ2w'><q id='XaZ2w'><span id='XaZ2w'><b id='XaZ2w'><form id='XaZ2w'><ins id='XaZ2w'></ins><ul id='XaZ2w'></ul><sub id='XaZ2w'></sub></form><legend id='XaZ2w'></legend><bdo id='XaZ2w'><pre id='XaZ2w'><center id='XaZ2w'></center></pre></bdo></b><th id='XaZ2w'></th></span></q></dt></tr></i><div id='XaZ2w'><tfoot id='XaZ2w'></tfoot><dl id='XaZ2w'><fieldset id='XaZ2w'></fieldset></dl></div>
        <bdo id='XaZ2w'></bdo><ul id='XaZ2w'></ul>

            <tbody id='XaZ2w'></tbody>

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

                本文介绍了如何擦除 &amp;删除指向存储在向量中的对象的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个向量,它存储指向许多动态实例化的对象的指针,我试图遍历该向量并删除某些元素(从向量中删除并销毁对象),但是我遇到了麻烦.这是它的样子:

                I have a vector that stores pointers to many objects instantiated dynamically, and I'm trying to iterate through the vector and remove certain elements (remove from vector and destroy object), but I'm having trouble. Here's what it looks like:

                    vector<Entity*> Entities;
                    /* Fill vector here */
                    vector<Entity*>::iterator it;
                    for(it=Entities.begin(); it!=Entities.end(); it++)
                        if((*it)->getXPos() > 1.5f)
                            Entities.erase(it);
                

                当任何实体对象达到 xPos>1.5 时,程序会因断言错误而崩溃...有人知道我做错了什么吗?

                When any of the Entity objects get to xPos>1.5, the program crashes with an assertion error... Anyone know what I'm doing wrong?

                我使用的是 VC++ 2008.

                I'm using VC++ 2008.

                推荐答案

                你需要小心,因为 erase() 将使现有的迭代器失效.但是,它将返回一个您可以使用的新的有效迭代器:

                You need to be careful because erase() will invalidate existing iterators. However, it will return a new valid iterator you can use:

                for ( it = Entities.begin(); it != Entities.end(); ) {
                   if( (*it)->getXPos() > 1.5f ) {
                      delete * it;  
                      it = Entities.erase(it);
                   }
                   else {
                      ++it;
                   }
                }
                

                这篇关于如何擦除 &amp;删除指向存储在向量中的对象的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:为什么 std::vector::operator[] 比 std::vector::at() 快 5 下一篇:在地图中寻找最大值

                相关文章

                最新文章

                  1. <small id='czjXG'></small><noframes id='czjXG'>

                  2. <tfoot id='czjXG'></tfoot>

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