473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Aug 29 '06 #1
6 9018

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

Aug 29 '06 #2
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::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.
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

Aug 29 '06 #3
mlimber wrote:
// 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

Aug 29 '06 #4
Philip Potter <ph***********@xilinx.comwrote:
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.
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.
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
Aug 29 '06 #5
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>

Aug 29 '06 #6
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>
Aug 29 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dylan | last post by:
At the moment I often find myself doing something like the following std::list<MyClass*> mcl; /.../fill mcl with some elements //now call aMethod() for each element; for...
12
by: sashan | last post by:
Sometimes I write code using std::for_each. Sometimes I use it on several container classes that are the same size. for_each(v.begin(), v.end(), f1()); for_each(u.begin(), u.end(), f2()); ...
2
by: psujkov | last post by:
Hi everybody, does anyone knows is there any way to have an implicit iteration counter in std::for_each ? e.g. std::for_each(c.begin(), c.end(), boost::bind(&C::set_id, _1,...
22
by: yurec | last post by:
Hi I wanna give template method of library class with some predicate, which I put to std::for_each in that method.How to break the std::for_each with predicate?(exceptions are used olny in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.