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

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

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

      2. 删除循环内向量的元素

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

        2. <small id='A9qWt'></small><noframes id='A9qWt'>

            • <bdo id='A9qWt'></bdo><ul id='A9qWt'></ul>

                1. 本文介绍了删除循环内向量的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我知道有与这个类似的问题,但我没能在他们的帮助下找到我的代码的方法.我只想通过检查循环内此元素的属性来删除/删除向量的元素.我怎样才能做到这一点?我尝试了以下代码,但收到了模糊的错误消息:

                  I know that there are similar questions to this one, but I didn’t manage to find the way on my code by their aid. I want merely to delete/remove an element of a vector by checking an attribute of this element inside a loop. How can I do that? I tried the following code but I receive the vague message of error:

                  'operator =' 功能在播放器"中不可用.

                  'operator =' function is unavailable in 'Player’.

                   for (vector<Player>::iterator it = allPlayers.begin(); it != allPlayers.end(); it++)
                   {
                       if(it->getpMoney()<=0) 
                           it = allPlayers.erase(it);
                       else 
                           ++it;
                   }
                  

                  我该怎么办?

                  更新:你认为问题vector::erase with pointer成员属于同样的问题?因此我需要赋值运算符吗?为什么?

                  Update: Do you think that the question vector::erase with pointer member pertains to the same problem? Do I need hence an assignment operator? Why?

                  推荐答案

                  你不应该在 for 循环中增加 it :

                  You should not increment it in the for loop:

                  for (vector<Player>::iterator it=allPlayers.begin(); 
                                                it!=allPlayers.end(); 
                                                /*it++*/) <----------- I commented it.
                  {
                  
                     if(it->getpMoney()<=0) 
                        it = allPlayers.erase(it);
                    else 
                        ++it;
                   }
                  

                  注意注释部分;it++ 在那里不需要,因为 it 在 for-body 本身中递增.

                  Notice the commented part;it++ is not needed there, as it is getting incremented in the for-body itself.

                  至于'operator =' function isavailable in 'Player'"的错误,来自于内部使用erase()的使用operator= 移动向量中的元素.为了使用erase()Player类的对象必须是可赋值的,这意味着你需要为实现operator=播放器类.

                  As for the error "'operator =' function is unavailable in 'Player’", it comes from the usage of erase() which internally uses operator= to move elements in the vector. In order to use erase(), the objects of class Player must be assignable, which means you need to implement operator= for Player class.

                  无论如何,您应该避免原始循环1 尽可能多,应该更喜欢使用算法.在这种情况下,流行的 Erase-Remove Idiom 可以简化您的操作正在做.

                  Anyway, you should avoid raw loop1 as much as possible and should prefer to use algorithms instead. In this case, the popular Erase-Remove Idiom can simplify what you're doing.

                  allPlayers.erase(
                      std::remove_if(
                          allPlayers.begin(), 
                          allPlayers.end(),
                          [](Player const & p) { return p.getpMoney() <= 0; }
                      ), 
                      allPlayers.end()
                  ); 
                  

                  1.这是我看过的Sean Parent 的最佳演讲之一.

                  这篇关于删除循环内向量的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:替代 vector<bool> 下一篇:如何从 std::vector&lt;&gt; 中擦除元素按索引

                  相关文章

                  最新文章

                  <small id='5Rz6j'></small><noframes id='5Rz6j'>

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

                  • <bdo id='5Rz6j'></bdo><ul id='5Rz6j'></ul>

                  1. <tfoot id='5Rz6j'></tfoot><legend id='5Rz6j'><style id='5Rz6j'><dir id='5Rz6j'><q id='5Rz6j'></q></dir></style></legend>