for_each problem

E

Ethan

class A {
void test(int k);
};

class Test {
A* d_a;
vector<int> d_array;

void test() {
typedef vector<int>::iterator VI;
for (VI i=d_array.begin(); i != d_array.end(); ++i)
{
d_a ->test(*i);
}
}
};

Question:
How can I use for_each to replace this "for" loop in test()?
for_each(d_array.begin(),
d_array.end(),
mem_fun(A::test)
);
does not work.

Please help,

Thanks,
 
V

Victor Bazarov

Ethan said:
class A {
void test(int k);
};

class Test {
A* d_a;
vector<int> d_array;

void test() {
typedef vector<int>::iterator VI;
for (VI i=d_array.begin(); i != d_array.end(); ++i)
{
d_a ->test(*i);
}
}
};

Question:
How can I use for_each to replace this "for" loop in test()?
for_each(d_array.begin(),
d_array.end(),
mem_fun1(A::test)
);
does not work.

You need to use 'bind1st' and all that stuff, like

for_each(d_array.begin(), d_array.end(),
bind1st(mem_fun1(&A::test), d_a) );

Or something like that...

V
 
A

Arne Mertz

Ethan said:
class A {
void test(int k);
};

class Test {
A* d_a;
vector<int> d_array;

void test() {
typedef vector<int>::iterator VI;
for (VI i=d_array.begin(); i != d_array.end(); ++i)
{
d_a ->test(*i);
}
}
};

Question:
How can I use for_each to replace this "for" loop in test()?
for_each(d_array.begin(),
d_array.end(),
mem_fun(A::test)
);
does not work.

"does not work" is no good description of what happens.
Please help,
mem_fun together with for_each means you call the member function of
each of the objects. Since ints have no member function A::test,
this will not work.
You'll have to provide your own functor, or search for boost::bind
or boost::lambda.

greets
A
 
J

Jerry Coffin

class A {
void test(int k);
};

class Test {
A* d_a;
vector<int> d_array;

void test() {
typedef vector<int>::iterator VI;
for (VI i=d_array.begin(); i != d_array.end(); ++i)
{
d_a ->test(*i);
}
}
};

Question:
How can I use for_each to replace this "for" loop in test()?

Minor nit: right now, A::test is private, so Test::test won't compile.
Most people find it all too easy to imitate that particular behavior,
but prefer not to do so. :)

Less minor nit: right now, you've defined d_a as a pointer to an A, but
you have not initialized it to point at anything -- when you attempt to
dereference it, you get undefined behavior -- another of those things
people find it all too easy to do, but generally attempt to avoid.
for_each(d_array.begin(),
d_array.end(),
mem_fun(A::test)
);

First you have to make the member function public, of course. Then you
have to have an actual obect on which to invoke the member function, not
just a pointer to nowhere of the right type.

Then, if possible, you rename the member function from 'test' to
'operator()' -- that makes invoking it really easy, and at least as
you've posted it, with A having only one member function, you might as
well use that name:

std::for_each(d_array.begin(), d_array.end(), A());

I'll let somebody else deal with the ugliness that arises when you
insist on giving the member function a different name -- quite a while
ago, I decided that was a great deal of ugliness and work with
essentially no benefit. If you decide you want to do it anyway, there
are some additions to <functional> in TR1 and the upcoming revision to
the C++ standard that make it a bit easier than what's in C++ 03.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,208
Latest member
RandallLay

Latest Threads

Top