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

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 ;) )

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


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

 
DonJuan

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;

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


...is 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.)

 
davepermen

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

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


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

 
Chris

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

 
Chris

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.