mem-fun

C++ To call member function in for_each for items in the member container

徘徊边缘 提交于 2019-12-23 12:09:51
问题 If I have a class (that mimic some of STL's container) like this: class Elem { public: void prepare(); // do something on *this // ... }; class Selector { public: typedef vector<Elem *> container_type; typedef container_type::iterator iterator; iterator begin() { return cont_.begin(); } iterator end() { return cont_.end(); } void check_all(); private: prepare_elem(Elem *p); // do something on 'p' container_type cont_; }; If I want to call prepare() for all elements in 'cont_', I could make

mem_fun and bind1st problem

本秂侑毒 提交于 2019-12-20 03:07:50
问题 I've following class: class A { public: // ctr and etc ... A* clone(B* container); }; Now, I've a vector<A*> availableObjs populated already. I want to call clone on each of those, so and insert cloned objects into a new container clonedObjs of type vector<A*> . I'm trying following - but it doesn't compile: transform(availableObjs.begin(), availableObjs.end(), back_inserter(clonedObjs), bind1st(mem_fun(&A::clone), container)); // container is of type B* Is there a easy way out? I've a lot

mem_fun and bind1st problem

那年仲夏 提交于 2019-12-01 23:05:57
I've following class: class A { public: // ctr and etc ... A* clone(B* container); }; Now, I've a vector<A*> availableObjs populated already. I want to call clone on each of those, so and insert cloned objects into a new container clonedObjs of type vector<A*> . I'm trying following - but it doesn't compile: transform(availableObjs.begin(), availableObjs.end(), back_inserter(clonedObjs), bind1st(mem_fun(&A::clone), container)); // container is of type B* Is there a easy way out? I've a lot classed like A - so making each of those a functor is too much task. You need to use bind2nd instead of