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

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

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

        对象向量上的 C++ remove_if

        时间:2023-09-15

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

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

              <legend id='oI1iA'><style id='oI1iA'><dir id='oI1iA'><q id='oI1iA'></q></dir></style></legend>
              <i id='oI1iA'><tr id='oI1iA'><dt id='oI1iA'><q id='oI1iA'><span id='oI1iA'><b id='oI1iA'><form id='oI1iA'><ins id='oI1iA'></ins><ul id='oI1iA'></ul><sub id='oI1iA'></sub></form><legend id='oI1iA'></legend><bdo id='oI1iA'><pre id='oI1iA'><center id='oI1iA'></center></pre></bdo></b><th id='oI1iA'></th></span></q></dt></tr></i><div id='oI1iA'><tfoot id='oI1iA'></tfoot><dl id='oI1iA'><fieldset id='oI1iA'></fieldset></dl></div>
                    <tbody id='oI1iA'></tbody>
                1. 本文介绍了对象向量上的 C++ remove_if的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个对象的向量(顺序很重要)(我们称它们为 myobj 类),我试图一次删除多个对象.

                  I have a vector (order is important) of objects (lets call them myobj class) where I'm trying to delete multiple objects at a time.

                  class vectorList
                  {
                  
                      vector<*myobj> myList; 
                  };
                  
                  class myobj
                  {
                  
                      char* myName;
                      int index;
                      bool m_bMarkedDelete;
                  }
                  

                  我认为最好的方法是标记要删除的特定 myobj 对象,然后在向量上调用 myList.remove_if().但是,我不确定如何为此使用谓词等.我应该在对象中创建一个成员变量,让我说我想删除 myobj 然后创建一个谓词来检查是否设置了成员变量?

                  I was thinking that the best way to do this would be to mark specific myobj objects for deletion and then call myList.remove_if() on the vector. However, I'm not exactly sure how to use predicates and such for this. Should I create a member variable in the object which allows me to say that I want to delete the myobj and then create a predicate which checks to see if the member variable was set?

                  如何将谓词实现为 vectorList 类的一部分?

                  How do I implement the predicate as a part of the vectorList class?

                  推荐答案

                  我应该在对象中创建一个允许我说的成员变量吗我想删除 myobj 然后创建一个谓词检查成员变量是否设置?

                  Should I create a member variable in the object which allows me to say that I want to delete the myobj and then create a predicate which checks to see if the member variable was set?

                  你不是已经这样做了吗?这不是 m_bMarkedDelete 的用途吗?你可以这样写谓词:

                  Haven't you already done that? Isn't that what m_bMarkedDelete is for? You would write the predicate like this:

                  bool IsMarkedToDelete(const myobj & o)
                  {
                      return o.m_bMarkedDelete;
                  }
                  

                  那么:

                  myList.erase(
                      std::remove_if(myList.begin(), myList.end(), IsMarkedToDelete),
                      myList.end());
                  

                  或者,使用 lambda:

                  Or, using lambdas:

                  myList.erase(
                      std::remove_if(myList.begin(), myList.end(),
                          [](const myobj & o) { return o.m_bMarkedDelete; }),
                      myList.end());
                  

                  如果你的班级实际上没有那个成员,而你问我们是否应该有,那么我会说没有.您使用什么标准来决定将其标记为删除?在谓词中使用相同的条件,例如:

                  If your class doesn't actually have that member, and you're asking us if it should, then I would say no. What criteria did you use to decide to mark it for deletion? Use that same criteria in your predicate, for example:

                  bool IndexGreaterThanTen(const myobj & o)
                  {
                      return o.index > 10;
                  }
                  

                  note -- 我写的函数当然是无效的,因为你所有的成员都是私有的.因此,您需要某种方式来访问它们.

                  note -- The functions I've written are of course invalid since all your members are private. So you'll need some way to access them.

                  这篇关于对象向量上的 C++ remove_if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:转换向量&lt;int&gt;串起来 下一篇:使用布尔值向量是否比动态位集慢?

                  相关文章

                  最新文章

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

                  <tfoot id='RAjCq'></tfoot>

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

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