Not logged in, Join Here! or Log In Below:  
News Articles Search    

 Home / General Programming / for_each & code readability Account Manager
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.

May 24, 2005, 11:38 AM

Hi !

Currently, I'am coding a nice C++ library, and I want a nice code, easy to read. Since I discovered the 'for_each' template, I use it, on the non critical parts. But is it really nice for everybody ?
Just a real life example (from my library ;) )

  2. struct PutOperatorsFunctor {
  3.   set<Operator*>* operators;
  4.   const set<Operator*>* builtInOperators;
  6.   PutOperatorsFunctor(set<Operator*>& operators, const set<Operator*>& builtInOperators) {
  7.     this->operators= &operators;
  8.     this->builtInOperators = &builtInOperators;
  9.   }
  11.   void operator() (map<string, Operator*>::value_type mapEntry) {
  12.     set<Operator*>::iterator it = builtInOperators->find(mapEntry.second);
  14.     if (it == builtInOperators->end())
  15.       operators->insert(mapEntry.second);
  16.   }
  17. }; // PutOperatorsFunctor
  21. for_each(session.operatorsMap.begin(),
  22.          session.operatorsMap.end(),
  23.          PutOperatorsFunctor(operatorsToDisplay, session.builtInOperators));

This is easier to read rather than the classical 'for' way ?


May 24, 2005, 11:48 AM

damn that's ugly.

Victor Widell

May 24, 2005, 12:00 PM

I did this chise a while ago after I started coding in PHP. Since PHP has foreach-loops as a part of the language, it is extremely convenient to loop through each element in an array. I use it all the time.

But when I tried to use this new found habit in C++, I came to the conclusion that the reccomended method;

  2. for(container<type>::iterator i=myContainer.begin(); i!=myContainer.end(); i++)
  3. much cleaner, simplier and more flexible than other foreach solutions I tried. (You can also just use the []operator if you know it will allways be a vector.)


May 24, 2005, 01:41 PM

i've once made a macro that allowed me to for_each over all containers in a way looking similar to other languages.. something like

  2. for_each<int>(item,items)
  3. {
  4.     cout<<item<<endl;
  5. }

or similar, but as i now work in c# for "real work", i don't have the old code anymore..


May 24, 2005, 04:48 PM

  1. for(container<type>::iterator i=myContainer.begin(); i!=myContainer.end(); i++)

This has the disadvantage of myContainer.end() being called once per iteration. If you know the container's content doesn't change, it's better to evaluate it before the loop.

I think some nice things can be done with boost's for_each and lambda expressions ? Does anyone have any small examples ready ?

Victor Widell

May 25, 2005, 10:54 AM

"disadvantage of myContainer.end() being called once per iteration"

And? It is just an inlined function returning the stored pointer anyway...

(More precisely, it should be if the implementation is any good.)


May 25, 2005, 11:47 AM

Of couse, if end() is trivial (which it hopefully is). My loops commonly look exactly like your proposal. I just thought I'd add that bit of comment.

This thread contains 7 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.