std::for_each



 DEVELOP > c-Plus-Plus > std::for_each

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Philip Potter"
Date: 29 Aug 2006 08:22:58 AM
Object: std::for_each
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::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
.

User: "Marcus Kwok"

Title: Re: std::for_each 29 Aug 2006 08:56:26 AM
Philip Potter <philip.potter@xilinx.com> wrote:

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::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?

If you have defined
std::ostream& operator<<(std::ostream&, const YourObj&);
then for a std::vector<YourObj> you can do:
#include <algorithm>
#include <iterator>
std::vector<YourObj> obj_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.

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<int> i_vect;
std::vector<double> d_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<IntDoubleStruct> wasn't the best idea.

Is there a textbook which covers good (moral) usage of the members of
<algorithm> and <functional>?

Not sure on specific book recommendations. Maybe check out the reviews
on http://accu.org/ .
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
.

User: "mlimber"

Title: Re: std::for_each 29 Aug 2006 08:47:07 AM
Philip Potter wrote:

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::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?

// For some T with operator<<(ostream&, const T&) defined...
std::vector<T> v;
// ...
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
<algorithm> for all but the simplest loop bodies since the standard
facilities aren't exceedingly readable IMHO.

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>?

Scott Meyers' _Effective STL_ is one.
Cheers! --M
.
User: "mlimber"

Title: Re: std::for_each 29 Aug 2006 08:48:46 AM
mlimber wrote:

// For some T with operator<<(ostream&, const T&) defined...
std::vector<T> v;
// ...
std::copy( v.begin(), v.end(), std::output_iterator<T>( cout ) );

That should be std::ostream_iterator<T>.
Cheers! --M
.


User: "Jon Clements"

Title: Re: std::for_each 29 Aug 2006 08:46:09 AM
Philip Potter wrote:
[snip]

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::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?

[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.
.
User: "Gernot Frisch"

Title: Re: std::for_each 29 Aug 2006 09:51:05 AM

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>
.
User: "Shooting"

Title: Re: std::for_each 29 Aug 2006 10:50:49 AM
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:

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>

.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER