2

I have a class like this

class Foo {
    public:
        int GetID() const { return m_id; }
    private:
        int m_id;
};

I want to use find_if on a vector full of Foo objects, like this:

std::find_if(foos.begin(), foos.end(), ???.GetID() == 42);

I don't quite get what I have to write instead of ???. I know there is either _1 with boost::lambda or arg1 with boost::phoenix, but after that I'm at a loss.

Edit:

I tried the following which does not work, even though I saw it like this in many examples.

std::find_if(foos.begin(), foos.end(), boost::lambda::bind(&Foo::GetID, boost::lambda::_1) == 42);

Then I get the following error:

error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const boost::lambda::lambda_functor' (or there is no acceptable conversion)

gartenriese
  • 4,131
  • 6
  • 36
  • 60

1 Answers1

4

Use boost::bind for this case, it's really the easiest way before C++11.

std::find_if(foos.begin(), foos.end(), boost::bind(&Foo::GetId, _1) == 42);

If you should use phoenix you can use phoenix::bind.

std::find_if(foos.begin(), foos.end(),
phx::bind(&Foo::GetId, phx::placeholders::_1) == 42);

where phx is

namespace phx = boost::phoenix;

If you can use C++11 - you can use std::bind, or lambda function

std::find_if(foos.begin(), foos.end(), 
[](const Foo& f) { return f.GetId() == 42; });
ForEveR
  • 55,233
  • 2
  • 119
  • 133