1

I have std::list<MyClass> *info1 I want to use (*info1).remove_if with a bool function of MyClass. As I see, everyone creating external functions or structs for remove_if. Isn't it possible to use it with bool MyClass::Condition() function?

Like this:

 class MyClass
{
private:
    int value;
public:
    bool Condition()
    {
      return ( value > 7500);
    }
};

And Then

   (*info1).remove_if(Condition())`
banderson
  • 65
  • 1
  • 11

3 Answers3

2

It is possible via std::mem_fn:

info1->remove_if(std::mem_fn(&MyClass::Condition));

Live demo

or via std::bind:

using namespace std::placeholders;
info1->remove_if(std::bind(&MyClass::Condition, _1));

Live demo

Both of those functions are included in the standard <functional> header.

Shoe
  • 74,840
  • 36
  • 166
  • 272
-2

for what you want to do it's preferable to user std algorithm

std::remove_if(info1->begin(), info1->end(),[](MyClass x){return x.Condition();})
  • Erm, [`std::list::remove_if`](http://en.cppreference.com/w/cpp/container/list/remove) is a standard algorithm. – Shoe May 26 '15 at 07:58
  • yes it's reimplementation of the std::remove_if – gregory boero.teyssier May 26 '15 at 08:03
  • `std::list::remove_if` exists for a reason. – T.C. May 26 '15 at 08:27
  • it's architectural c++ error for lazy developers – gregory boero.teyssier May 26 '15 at 08:48
  • @gregoryboero.teyssier No, they exist because they are more efficient than the standalone algorithms. – T.C. May 26 '15 at 18:51
  • @T.C.i think you're wrong but test the following code on you'r IDE i put my output in coms and the why the difference is so big http://coliru.stacked-crooked.com/a/95666d88e9f41695 – gregory boero.teyssier May 27 '15 at 08:50
  • That's because `list::remove_if` erases the removed elements from the list, while `std::remove_if` shifts the non-removed elements to the front, leaving the remaining elements in an unspecified state. What you are seeing is the time cost of the deallocating the nodes. If you were to do the same thing using `std::remove_if`, you'd have to use the erase-remove idiom. Also, you picked `int`s, whose assignment (which is what what `list::remove_if` avoids since it just relinks the nodes) is really cheap. If you used more complicated elements (like a large `std::array`), it would be very different. – T.C. May 28 '15 at 05:08
  • @T.C.did you try before comment what you say about erase-remove idiom is true but did you try with it . 'listInt.erase(remove_if(listInt.begin(), listInt.end(), [](int x){return (x % 2) == 1; }), listInt.end());' .because the execution time is still less than list::remove_if (much less) – gregory boero.teyssier May 28 '15 at 08:36
  • Really? [erase-remove - 598ms](http://coliru.stacked-crooked.com/a/603ac16b7b448c24), [list::remove_if - 370ms](http://coliru.stacked-crooked.com/a/c10909def0b86e07)? Did you turn on optimizations, considering that your reported run time numbers are way higher than Coliru's? – T.C. May 29 '15 at 01:32
  • yes i try with the code your made and i have the same result than before erase-remove 1636 ms and 7125 ms for list::remove_if to be sure i created a new console project in 64bit i don't know how Coliru work – gregory boero.teyssier May 29 '15 at 08:10
-2

You can use add an operator() in MyClass and use it as a predicate here is an example:

#include <iostream>
#include <list>

using namespace std;
class MyClass
{
  private:
    int value;
  public:
    MyClass(int val) : value(val) {}
    int get_value() const { return value; }
    bool operator()(const MyClass& M)
    {
      return (M.value > 7500);
    }
};

int main() {
  list<MyClass> v;
  v.emplace_back(100);
  v.emplace_back(10000);
  v.emplace_back(500);
  v.emplace_back(7500);
  v.emplace_back(50000);
  v.remove_if(MyClass(0));
  for (const auto& i : v) {
    cout << i.get_value() << "\n";
  }
  return 0;
}
Brahim
  • 808
  • 1
  • 8
  • 17
  • 1
    This is a bad idea because you are effectively using `MyClass` as a predicate and as something else (depending on the original meaning of `MyClass`). Especially if `MyClass` is not supposed to have "callable" semantics. – Shoe May 26 '15 at 08:02
  • Why did I have a -1 is this not working? I actually tested it. – Brahim May 26 '15 at 08:02