P
Philip Potter
Hello there,
I'm reading about the std::for_each() function in TC++PL, 3rd Ed. It seems
like a good idea, but in practice I can never see a way to bend it to my
wishes without writing huge function objects. The same goes for most things
in <algorithm>, though I don't know if I'm just not used to it or if it
really is ugly.
Specific question:
I have a vector of objects and I'd like to send them all to std::cout. Is
there a way of doing this using standard binders and adapters? I have tried
the following:
std::for_each(myvector.begin(), myvector.end(), std::cout.operator<<);
std::for_each(myvector.begin(), myvector.end(),
bind1st(mem_fun_ref(&ostream:
perator<<),std::cout));
...but that didn't work. Would it have worked if operator<<(ostream &, myobj)
was a regular function? Is there a clean, readable, terse way of writing
what I want to write?
General questions:
When do you prefer to use std::for_each(), when do you prefer to use a for
loop over iterators, and when do you prefer to use a for loop over indices?
Is there a textbook which covers good (moral) usage of the members of
<algorithm> and <functional>?
Philip
I'm reading about the std::for_each() function in TC++PL, 3rd Ed. It seems
like a good idea, but in practice I can never see a way to bend it to my
wishes without writing huge function objects. The same goes for most things
in <algorithm>, though I don't know if I'm just not used to it or if it
really is ugly.
Specific question:
I have a vector of objects and I'd like to send them all to std::cout. Is
there a way of doing this using standard binders and adapters? I have tried
the following:
std::for_each(myvector.begin(), myvector.end(), std::cout.operator<<);
std::for_each(myvector.begin(), myvector.end(),
bind1st(mem_fun_ref(&ostream:
...but that didn't work. Would it have worked if operator<<(ostream &, myobj)
was a regular function? Is there a clean, readable, terse way of writing
what I want to write?
General questions:
When do you prefer to use std::for_each(), when do you prefer to use a for
loop over iterators, and when do you prefer to use a for loop over indices?
Is there a textbook which covers good (moral) usage of the members of
<algorithm> and <functional>?
Philip