Sign in to follow this  

for_each

This topic is 3489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to print the elements of a vector line by line. This is what I have:
void print(const string& s) { cout << s << endl; }

int main()
{
    // ...
    for_each(v.begin(), v.end(), print);
}

Now is there any way to get rid of the function print? Does C++ have something like closures? I know I can do
    for (vector<string>::iterator it = v.begin(); it != v.end(); ++it)
        cout << *it << endl;

But is something like this possible with the for_each function?

Share this post


Link to post
Share on other sites
Look into boost::lambda

C++ doesn't (yet) have language support for closures (but afaik, it's on track to be added in C++0x). But until then, it can be emulated with a lot of template hackery. (or by letting boost handle it)

Share this post


Link to post
Share on other sites
Two options:
int main()
{
// ...
std::copy(v.begin(), v.end(), std::ostream_iterator< std::string >(std::cout, "\n"));
}

#include <boost/lambda/lambda.hpp>

int main()
{
// ...
using namespace boost::lambda;
std::for_each(v.begin(), v.end(), std::cout << _1 << '\n');
}

Σnigma

Share this post


Link to post
Share on other sites
Thanks for the replies!

That ostream_iterator seems to have a funny definition of operator* and operator= :-)

ostream_iterator<string> it(cout, "\n");
* ** *** **** ***** **** *** ** * it = "Hello World!";

Share this post


Link to post
Share on other sites
Take a look at Boost.Foreach.

Quote:

What is BOOST_FOREACH?

In C++, writing a loop that iterates over a sequence is tedious. We can either use iterators, which requires a considerable amount of boiler-plate, or we can use the std::for_each() algorithm and move our loop body into a predicate, which requires no less boiler-plate and forces us to move our logic far from where it will be used. In contrast, some other languages, like Perl, provide a dedicated "foreach" construct that automates this process. BOOST_FOREACH is just such a construct for C++. It iterates over sequences for us, freeing us from having to deal directly with iterators or write predicates.

BOOST_FOREACH is designed for ease-of-use and efficiency. It does no dynamic allocations, makes no virtual function calls or calls through function pointers, and makes no calls that are not transparent to the compiler's optimizer. This results in near-optimal code generation; the performance of BOOST_FOREACH is usually within a few percent of the equivalent hand-coded loop. And although BOOST_FOREACH is a macro, it is a remarkably well-behaved one. It evaluates its arguments exactly once, leading to no nasty surprises.


I usually

#define foreach BOOST_FOREACH

and use "foreach" instead, which is easier on the eye.

Example usage:

#define foreach BOOST_FOREACH

foreach( const std::string& str, v ) {
cout << str;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
Two options:
*** Source Snippet Removed ***
*** Source Snippet Removed ***
Σnigma

Yup that first approach is the one taken in my Deitel C++ book I used in the C++ I just took in college.
I always wondered if there was an easier way too. I guess not.

Share this post


Link to post
Share on other sites

This topic is 3489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this