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

      <legend id='tOQOi'><style id='tOQOi'><dir id='tOQOi'><q id='tOQOi'></q></dir></style></legend>

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

      <tfoot id='tOQOi'></tfoot>
          <bdo id='tOQOi'></bdo><ul id='tOQOi'></ul>

        从 C++ std::vector 中删除元素

        时间:2023-10-07
        <tfoot id='52UNv'></tfoot>
        • <small id='52UNv'></small><noframes id='52UNv'>

            <bdo id='52UNv'></bdo><ul id='52UNv'></ul>

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

                  本文介绍了从 C++ std::vector 中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在迭代过程中从 C++ 向量中删除元素的正确方法是什么?我正在迭代一个数组并想删除一些匹配特定条件的元素.有人告诉我,在遍历期间修改它是一件坏事.

                  What is the proper way to remove elements from a C++ vector while iterating through it? I am iterating over an array and want to remove some elements that match a certain condition. I've been told that it's a bad thing to modify it during traversal.

                  我想我还应该提到这是一个指针数组,我需要在删除它们之前释放它们.

                  I guess I should also mention that this is an array of pointers that I need to free before removing them.

                  这是我的代码片段.

                  
                  void RoutingProtocolImpl::removeAllInfinity()
                  {
                    dv.erase(std::remove_if(dv.begin(), dv.end(), hasInfCost), dv.end()); 
                  }
                  
                  bool RoutingProtocolImpl::hasInfCost(RoutingProtocolImpl::dv_entry *entry)
                  {
                    if (entry->link_cost == INFINITY_COST)
                    {
                      free(entry);
                      return true;
                    }
                    else
                    {
                      return false;
                    }
                  }
                  
                  

                  编译时出现以下错误:

                  
                  RoutingProtocolImpl.cc:368: error: argument of type bool (RoutingProtocolImpl::)(RoutingProtocolImpl::dv_entry*)' does not matchbool (RoutingProtocolImpl::)(RoutingProtocolImpl::dv_entry)'
                  

                  抱歉,我是 C++ 新手.

                  Sorry, I'm kind of a C++ newb.

                  推荐答案

                  bool IsEven (int i) 
                  { 
                    return (i%2) == 0; 
                  }
                  
                  //...
                  
                  std::vector<int> v;
                  v.push_back(1);
                  v.push_back(2);
                  v.push_back(3);
                  v.push_back(4);
                  v.erase(std::remove_if(v.begin(),v.end(),IsEven), v.end()); 
                  //v now contains 1 and 3
                  

                  这篇关于从 C++ std::vector 中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:获取 STL 向量中大于某个值的元素的所有位置 下一篇:容量是否复制到向量中?

                  相关文章

                  最新文章

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

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