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

      <tfoot id='3dwdu'></tfoot><legend id='3dwdu'><style id='3dwdu'><dir id='3dwdu'><q id='3dwdu'></q></dir></style></legend>
          <bdo id='3dwdu'></bdo><ul id='3dwdu'></ul>

      1. <small id='3dwdu'></small><noframes id='3dwdu'>

      2. 迭代时从向量中删除项目?

        时间:2024-05-12
      3. <legend id='PKnZa'><style id='PKnZa'><dir id='PKnZa'><q id='PKnZa'></q></dir></style></legend>

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

            • <small id='PKnZa'></small><noframes id='PKnZa'>

              <tfoot id='PKnZa'></tfoot>

              • <i id='PKnZa'><tr id='PKnZa'><dt id='PKnZa'><q id='PKnZa'><span id='PKnZa'><b id='PKnZa'><form id='PKnZa'><ins id='PKnZa'></ins><ul id='PKnZa'></ul><sub id='PKnZa'></sub></form><legend id='PKnZa'></legend><bdo id='PKnZa'><pre id='PKnZa'><center id='PKnZa'></center></pre></bdo></b><th id='PKnZa'></th></span></q></dt></tr></i><div id='PKnZa'><tfoot id='PKnZa'></tfoot><dl id='PKnZa'><fieldset id='PKnZa'></fieldset></dl></div>
                    <tbody id='PKnZa'></tbody>
                  本文介绍了迭代时从向量中删除项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个向量,其中包含活动或非活动的项目.我希望此向量的大小保持较小以解决性能问题,因此我希望从向量中删除已标记为非活动的项目.我在迭代时尝试这样做,但我收到错误向量迭代器不兼容".

                  I have a vector that holds items that are either active or inactive. I want the size of this vector to stay small for performance issues, so I want items that have been marked inactive to be erased from the vector. I tried doing this while iterating but I am getting the error "vector iterators incompatible".

                  vector<Orb>::iterator i = orbsList.begin();
                  
                      while(i != orbsList.end()) {
                          bool isActive = (*i).active;
                  
                          if(!isActive) {
                              orbsList.erase(i++);
                          }
                          else {
                              // do something with *i
                              ++i;
                          }
                      }
                  

                  推荐答案

                  过去我做过的最易读的方法是使用 std::vector::erase 结合 std::remove_if.在下面的示例中,我使用此组合从向量中删除任何小于 10 的数字.

                  The most readable way I've done this in the past is to use std::vector::erase combined with std::remove_if. In the example below, I use this combination to remove any number less than 10 from a vector.

                  (对于非 c++0x,您可以将下面的 lambda 替换为您自己的谓词:)

                  // a list of ints
                  int myInts[] = {1, 7, 8, 4, 5, 10, 15, 22, 50. 29};
                  std::vector v(myInts, myInts + sizeof(myInts) / sizeof(int));
                  
                  // get rid of anything < 10
                  v.erase(std::remove_if(v.begin(), v.end(), 
                                         [](int i) { return i < 10; }), v.end());
                  

                  这篇关于迭代时从向量中删除项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:std:map 中的浮点键 下一篇:如何更新 std::set 的现有元素?

                  相关文章

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

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

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

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