开发者

Call c++ member function with each element in a list?

开发者 https://www.devze.com 2023-01-15 20:26 出处:网络
I have a list of Thing and a Controller that I want to notify() with each of the things. The code below works:

I have a list of Thing and a Controller that I want to notify() with each of the things. The code below works:

#include <algorithm>
#include <iostream>
#include <tr1/functional>
#include <list>
using namespace std;

class Thing { public: int x; };

class Controller
{
public:
    void notify(Thing& t) { cerr << t.x << endl; }
};

class Notifier
{
public:
    Notifier(Controller* c) { _c = c; }
    void operator()(Thing& t) { _c->notify(t); }
private:
    Controller* _c;
};

int main()
{
    list<Thing> things;
    Controller c;

    // ... add some things ...
    Thing t;
    t.x = 1; things.push_back(t);
    t.x = 2; things.push_back(t);
    t.x = 3; things.push_back(t);

    // This doesn't work:
    //for_each(things.begin(), things.end(),
    //         tr1::mem_fn(&Controller::notify));

    for_each(things.begin(), things.end(), Notifier(&c));
    return 0;
}

My question is: can I get rid of the Notifier class by using some version of the "This doesn't work" line? Seems like I should be able to make something work, but can't quite get the right combination. (I've fumbled around with a number of different combinations.)

Without using boost? (I would if I could.) I'm using g++ 4.1.2, yes I kno开发者_如何学编程w it is old...


You can accomplish this using bind, which is originally from Boost but is included in TR1 and C++0x:

using std::tr1::placeholders::_1;
std::for_each(things.begin(), things.end(),
              std::tr1::bind(&Controller::notify, c, _1));


What about going old-school:

for(list<Thing>::iterator i = things.begin(); i != things.end(); i++)
  c.notify(*i);
0

精彩评论

暂无评论...
验证码 换一张
取 消