Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What's the advantage of using for_each loop?

Reply
Thread Tools

What's the advantage of using for_each loop?

 
 
Sarath
Guest
Posts: n/a
 
      05-30-2007
What's the advantage of using for_each than the normal iteration using
for loop?

Is there any 'tweak' to use stream objects as 'Function' parameter of
for_each loop. Seems copy function can do the same.

 
Reply With Quote
 
 
 
 
v.r.marinov@gmail.com
Guest
Posts: n/a
 
      05-30-2007
> What's the advantage of using for_each than the normal iteration using
> for loop?


In general for_each is supposed to be less verbose than for-loop.
The cool thing about for_each is:

1) you can pass the function objects by reference
2) you can apply side-effects on the function object
3) you can use the return value of the for_each() algorithm

> Is there any 'tweak' to use stream objects as 'Function' parameter of
> for_each loop. Seems copy function can do the same.


How about the boost library?
http://www.boost.org/

With std::copy you can do something like:

copy(a.begin(), a.end(), ostream_iterator<string>(cout, " ");


and with boost::lambda you can do the same thing:

for_each(a.begin(), a.end(), std::cout << _1 << ' ');

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      05-30-2007
Sarath <(E-Mail Removed)> wrote:

> What's the advantage of using for_each than the normal iteration using
> for loop?


I'm beginning to think this should be an FAQ.

A key benefit of the standard library algorithms is that they save
the programmer from writing explicit loops. Loops can be tedious and
error-prone. The for_each() algorithm is the simplest algorithm in
the sense that it does nothing but eliminate an explicit loop... One
common use of for_each() is to extract information from elements of a
sequence. -- Stroustrup

> Is there any 'tweak' to use stream objects as 'Function' parameter of
> for_each loop. Seems copy function can do the same.


Obviously, if a more specialized algorithm works, then use it instead.
 
Reply With Quote
 
Sarath
Guest
Posts: n/a
 
      05-30-2007
On May 30, 10:33 am, (E-Mail Removed) wrote:
> > What's the advantage of using for_each than the normal iteration using
> > for loop?

>
> In general for_each is supposed to be less verbose than for-loop.
> The cool thing about for_each is:
>
> 1) you can pass the function objects by reference
> 2) you can apply side-effects on the function object
> 3) you can use the return value of the for_each() algorithm
>
> > Is there any 'tweak' to use stream objects as 'Function' parameter of
> > for_each loop. Seems copy function can do the same.

>
> How about the boost library?http://www.boost.org/
>
> With std::copy you can do something like:
>
> copy(a.begin(), a.end(), ostream_iterator<string>(cout, " ");
>
> and with boost::lambda you can do the same thing:
>
> for_each(a.begin(), a.end(), std::cout << _1 << ' ');


Yea I used boost::lambda to do the same. But it's not possible to do
with ISO C++ STL implementation

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-30-2007
On May 30, 2:16 am, Sarath <(E-Mail Removed)> wrote:
> What's the advantage of using for_each than the normal iteration using
> for loop?


Obfuscation?

Seriously, the advantage of using for_each is that the reader
knows that the loop doesn't terminate prematurely. This has to
be weighed against the fact the code you execute each pass must
be separate---if the code is more or less general, and has a
good name, this is an advantage; if the code is specialized to
the place making the call, and has no good name, then it is bad.

Note too that any good coding guideline will forbid using break
or return in a loop anyway, which sort of negates the first
advantage.

> Is there any 'tweak' to use stream objects as 'Function' parameter of
> for_each loop. Seems copy function can do the same.


There is a standard class which converts streams to iterators.
Functions which need iterators can use them. Thus, they work
for the first two arguments of for_each, but not for the third.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


 
Reply With Quote
 
Jon Harrop
Guest
Posts: n/a
 
      05-30-2007
Sarath wrote:
> What's the advantage of using for_each than the normal iteration using
> for loop?


Brevity. The "for_each" function is a rudimentary form of higher-order
function from functional programming.

For example, the pedagogical C++ example:

copy(a.begin(), a.end(), ostream_iterator<string>(cout, " ");

May be written:

iter print a

in a functional language, where "iter" is a higher-order function equivalent
to "for_each" or "copy". However, functional programming languages like
Haskell, OCaml, F# and Standard ML are all much better at finding and
explaining errors at compile time.

--
Dr Jon D Harrop, Flying Frog Consultancy
OCaml for Scientists
http://www.ffconsultancy.com/product...ntists/?usenet
 
Reply With Quote
 
Robbie Hatley
Guest
Posts: n/a
 
      05-30-2007

<(E-Mail Removed)> wrote:

> The cool thing about for_each is:
> 1) you can pass the function objects by reference


I don't see how that could work. I thought the function
object is always passed by value? I tried passing a function
object to a for_each loop once, where the function object was
collecting data about operations. But that didn't work,
because for_each used a *copy* of the object, rather than the
object itself.

I was writing a spelling-checker, just for programming
practice. The for_each loop looked something like:

Append FuncObj = Append(Dictionary);
for_each(Dicts.begin(), Dicts.end(), FuncObj);

("Dicts" is a list of file paths, and "Dictionary" is a list
of words.)

But the the counts of files and lines went into the unnamed
*copy* of FuncObj, not FuncObj itself.

> 2) you can apply side-effects on the function object


?

> 3) you can use the return value of the for_each() algorithm


Hmmm.... Interesting. Perhaps, then, something like the following
would work in my spelling checker (could be used to count files --
or lines -- appended to a list of strings):

Append FuncObjCopy =
for_each(Dicts.begin(), Dicts.end(), Append(Dictionary);
cout << "Files processed = " << FuncObjCopy.files << endl;
cout << "Lines processed = " << FuncObjCopy.lines << endl;

Not quite as nice as getting for_each to use the original function
object, but almost.


--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
triple-dubya dott tustinfreezone dott org


 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      05-30-2007
On Wed, 30 May 2007 15:30:36 +0100, Jon Harrop wrote:
>
>The "for_each" function is a rudimentary form of higher-order
>function from functional programming.


The amazing thing is that in the course of its evolution C++ changed
the underlying paradigm from object-oriented to value-oriented,
functional. This 'paradigm shift' happened approximately in 1995 with
the inauguration of STL and caused a lot of confusion ever since.


--
Roland Pibinger
"I often think that the sole purpose of the STL is to see how many
lines one can use to obfuscate a simple loop." - John Potter
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-31-2007
On May 30, 5:02 pm, "Robbie Hatley" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> wrote:
> > The cool thing about for_each is:
> > 1) you can pass the function objects by reference


> I don't see how that could work. I thought the function
> object is always passed by value?


They are.

> I tried passing a function object to a for_each loop once,
> where the function object was collecting data about
> operations. But that didn't work, because for_each used a
> *copy* of the object, rather than the object itself.


> I was writing a spelling-checker, just for programming
> practice. The for_each loop looked something like:


> Append FuncObj = Append(Dictionary);
> for_each(Dicts.begin(), Dicts.end(), FuncObj);


> ("Dicts" is a list of file paths, and "Dictionary" is a list
> of words.)


> But the the counts of files and lines went into the unnamed
> *copy* of FuncObj, not FuncObj itself.


The STL requires functional objects to have value semantics, so
you would (generally) write something like:

Append FuncObj( for_each( Dicts.begin(),
Dicts.end(),
Append( Dictionary ) ) ) ;

The problem becomes more difficult if you are trying to use a
polymorphic object; you have to give it value semantics which do
not loose the polymorphic behavior, by means of the
letter/envelope idiom, or something similar. (Often, just a
small wrapper around a pointer will do the trick.)

> > 2) you can apply side-effects on the function object


> ?


The function object can do just about anything which won't
invalidate the iterators. Including modifying program state.

> > 3) you can use the return value of the for_each() algorithm


> Hmmm.... Interesting. Perhaps, then, something like the following
> would work in my spelling checker (could be used to count files --
> or lines -- appended to a list of strings):


> Append FuncObjCopy =
> for_each(Dicts.begin(), Dicts.end(), Append(Dictionary);
> cout << "Files processed = " << FuncObjCopy.files << endl;
> cout << "Lines processed = " << FuncObjCopy.lines << endl;


> Not quite as nice as getting for_each to use the original function
> object, but almost.


In some contexts (including this one, IMHO), it's even nicer.
In others (i.e. polymorphic agents), it's a real pain you know
where.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      05-31-2007
Robbie Hatley wrote:
> <(E-Mail Removed)> wrote:
>
>> The cool thing about for_each is:
>> 1) you can pass the function objects by reference

>
> I don't see how that could work. I thought the function
> object is always passed by value? I tried passing a function
> object to a for_each loop once, where the function object was
> collecting data about operations. But that didn't work,
> because for_each used a *copy* of the object, rather than the
> object itself.


There are things coming in the new C++ that will make this easier I
think. Would be something like so maybe:

std::for_each(itera, iterb, ref(f));

I think the committee is *trying* to get the std locked some time this year?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Microsoft SQL , Sybase advantage SQL,Advantage database Tommy Computer Support 1 11-29-2007 04:21 AM
Question about using for_each Yan C++ 3 07-12-2007 02:20 AM
Using for_each with a vector of vectors PolkaHead C++ 3 11-29-2006 12:58 AM
Using an object's member function as the action passed to for_each Belebele C++ 6 08-14-2006 03:35 PM
Using for_each() to deallocate memory Eric Lilja C++ 2 03-14-2005 02:51 PM



Advertisments