R
rile
I have this problem that I meet quite often.
I have a STL container with bunch of whatever objects. The container is
a member of a class that does something. Now, I want to apply a member
of this class to each of the contained objects.
mem_fun will CALL the given member function OF THE CONTAINED object,
but what I want is to call the given method of the CONTAINING object
with the argument being a contained object.
For example:
class Foo
{
public:
....
void doSomething(int i)
{
cout << i << endl;
}
void doSomethingWithEach()
{
for_each<myv.begin(), myv.end(), call_mem_fun(&Foo::doSomething,
this));
}
std::vector<int> myv;
};
NOTE:
call_mem_fun would call "doSomething" and pass to it each int from the
container.
================
So I created the "call_mem_fun" functor, but I'm not sure if I was
reinventing the wheel (just can not believe there is not such thing
already out there somewhere).
Any suggestions?
I have a STL container with bunch of whatever objects. The container is
a member of a class that does something. Now, I want to apply a member
of this class to each of the contained objects.
mem_fun will CALL the given member function OF THE CONTAINED object,
but what I want is to call the given method of the CONTAINING object
with the argument being a contained object.
For example:
class Foo
{
public:
....
void doSomething(int i)
{
cout << i << endl;
}
void doSomethingWithEach()
{
for_each<myv.begin(), myv.end(), call_mem_fun(&Foo::doSomething,
this));
}
std::vector<int> myv;
};
NOTE:
call_mem_fun would call "doSomething" and pass to it each int from the
container.
================
So I created the "call_mem_fun" functor, but I'm not sure if I was
reinventing the wheel (just can not believe there is not such thing
already out there somewhere).
Any suggestions?