Connecting Tech Pros Worldwide Help | Site Map

std::for_each

Philip Potter
Guest
 
Posts: n/a
#1: Aug 29 '06
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::operator<<),std::cou t));
...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
<algorithmand <functional>?

Philip

Jon Clements
Guest
 
Posts: n/a
#2: Aug 29 '06

re: std::for_each



Philip Potter wrote:
[snip]
Quote:
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::operator<<),std::cou t));
..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?
[snip]

I'll probably be shot down by others in the NG but I use the Boost
Lambda library at http://www.boost.org/doc/html/lambda.html. In fact,
what you're trying to achieve here is its first example.

Jon.

mlimber
Guest
 
Posts: n/a
#3: Aug 29 '06

re: std::for_each


Philip Potter wrote:
Quote:
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::operator<<),std::cou t));
..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?
// For some T with operator<<(ostream&, const T&) defined...
std::vector<Tv;
// ...
std::copy( v.begin(), v.end(), std::output_iterator<T>( cout ) );

Alternately, you could use boost::lambda and boost::bind (aka
std::tr1::bind) to make things more readable. In general, I avoid
<algorithmfor all but the simplest loop bodies since the standard
facilities aren't exceedingly readable IMHO.
Quote:
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
<algorithmand <functional>?
Scott Meyers' _Effective STL_ is one.

Cheers! --M

mlimber
Guest
 
Posts: n/a
#4: Aug 29 '06

re: std::for_each


mlimber wrote:
Quote:
// For some T with operator<<(ostream&, const T&) defined...
std::vector<Tv;
// ...
std::copy( v.begin(), v.end(), std::output_iterator<T>( cout ) );
That should be std::ostream_iterator<T>.

Cheers! --M

Marcus Kwok
Guest
 
Posts: n/a
#5: Aug 29 '06

re: std::for_each


Philip Potter <philip.potter@xilinx.comwrote:
Quote:
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::operator<<),std::cou t));
..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?
If you have defined

std::ostream& operator<<(std::ostream&, const YourObj&);

then for a std::vector<YourObjyou can do:

#include <algorithm>
#include <iterator>

std::vector<YourObjobj_vect;
// ...
std::copy(obj_vect.begin(),
obj_vect.end(),
std::ostream_iterator<YourObj>(std::cout, "\n"));

where "\n" specifies the delimiter to place between vector entries.
Quote:
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?
Honestly, I almost never use std::for_each(). I use iterators when I
can, unless there is a situation in which I have to deal with parallel
data structures for some reason. With parallel data structures, I mean
something like:

std::vector<inti_vect;
std::vector<doubled_vect;

for (std::vector<int>::size_type i = 0; i != i_vect.size(); ++i) {
something = i_vect[i] * d_vect[i];
}

where i_vect[i] and d_vect[i] both store some property about the same
logical entity, but for some design reason a
std::vector<IntDoubleStructwasn't the best idea.
Quote:
Is there a textbook which covers good (moral) usage of the members of
<algorithmand <functional>?
Not sure on specific book recommendations. Maybe check out the reviews
on http://accu.org/ .

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Gernot Frisch
Guest
 
Posts: n/a
#6: Aug 29 '06

re: std::for_each


Quote:
I'll probably be shot down by others in the NG but I use the Boost
Lambda library at http://www.boost.org/doc/html/lambda.html. In
fact,
what you're trying to achieve here is its first example.
taking a look at lambda: it looks rather easy for such purposes to do.
but... uhm... Can someone explain to me how they did it?
Is _1 a class that has all the +-*/ operators?
And how do they de-reference _1?
<blink blink>



Shooting
Guest
 
Posts: n/a
#7: Aug 29 '06

re: std::for_each


Yeah, but _1 is not a class, It's an object that has all the
+-*/...ect. operators. For those function can not specified by
operators, they also have their way to deal with such as new , delete
ect...
Gernot Frisch wrote:
Quote:
Quote:
I'll probably be shot down by others in the NG but I use the Boost
Lambda library at http://www.boost.org/doc/html/lambda.html. In
fact,
what you're trying to achieve here is its first example.
>
taking a look at lambda: it looks rather easy for such purposes to do.
but... uhm... Can someone explain to me how they did it?
Is _1 a class that has all the +-*/ operators?
And how do they de-reference _1?
<blink blink>
Closed Thread


Similar C / C++ bytes