• <bdo id='3z4Qy'></bdo><ul id='3z4Qy'></ul>

  • <small id='3z4Qy'></small><noframes id='3z4Qy'>

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

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

        Removing elements from C++ std::vector(从 C++ std::vector 中删除元素)

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

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

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

                • 本文介绍了从 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 中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)

                    1. <small id='2gTq8'></small><noframes id='2gTq8'>

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

                          <tfoot id='2gTq8'></tfoot>