   [C++0x] Stopping for_each in lambda
  I use the below to search for something.entity* pEnt = 0;std::for_each(p->mEntities.cbegin(),p->mEntities.cend(),[&](entity*& e){if (<found condition>){    // Got it.    pEnt = e;}});return pEnt;Naturally, I don't want to stick around in the loop if I found what I needed. What is the cleanest way to bust out of a for_each in this setup if 'break' is out of the question?Also, I don't know all the finer details of lambda yet, so inform me if mine can be better set up.   
  std::find_if seems to be for you. Or if you need more fine-tuning, what about for (auto it : p->mEntitties) {...}?   ;Quote:std::find_if seems to be for you.Sure is. Thanks.Quote: what about for (auto it : p->mEntitties) {...}?I would like that muchly, but I can not get that syntax to work in VC2010. I've tried it before. Not implemented yet?EDIT:It's supposed to befor each ( auto it in p->mEntities ) [Edited by - zyrolasting on April 22, 2010 9:19:56 AM];Quote: what about for (auto it : p->mEntitties) {...}?I would like that muchly, but I can not get that syntax to work in VC2010. I've tried it before. Not implemented yet?I only know that it is not scheduled for gcc yet :(Quote:EDIT:It's supposed to befor each ( auto it in p->mEntities )n3000 sez:Quote:6.5 Iterationstatements [stmt.iter]1 Iteration statements specify looping.iteration-statement:    while( condition ) statement    do statement while( expression );    for( for-init-statementconditionopt ; expressionopt ) statement    for( for-range-declaration : expression ) statementfor-init-statement:    expression-statement    simple-declarationfor-range-declaration:    type-speciﬁer-seqattribute-speciﬁeropt declarator[Note: a for-init-statement ends with a semicolon. —endnotefor each ( auto it in p->mEntities ) is more like what C# does (except that it's foreach and var then (sidenote: C++'s auto is better than C#'s var, as it also allows for for(auto it=begin(), end=end(); it!=end; ++it), whereas in C# you can only one variable per var))   ;Quote:Original post by zyrolastingI use the below to search for something.*** Source Snippet Removed ***Naturally, I don't want to stick around in the loop if I found what I needed. What is the cleanest way to bust out of a for_each in this setup if 'break' is out of the question?Also, I don't know all the finer details of lambda yet, so inform me if mine can be better set up.Note that this really has nothing to do with lambdas, it has to do with the fact that for_each just isn't designed for this.  Look at the code of for_each, all it does is iterate over everything and invoke the function.  But that's not what the code looks like in a loop where you can short circuit out of the iteration.  you could write your own quite easily however, and it would be a little bit different than for_each_if.  for_each_if will still iterate over every item in the list.  you need to copy the code of for_each / for_each_if and make a new function called like for_each_until and for_each_if_until.  then make the body of the actual for loop issue a break statement if the until predicate is true.;  Or you can use a loop...;Quote:Original post by alvaroOr you can use a loop...totally agree..   ;Quote:Original post by phresnelstd::find_if seems to be for you. Or if you need more fine-tuning, what about for (auto it : p->mEntitties) {...}?Off topic, but does the new for-each syntax mean that the begin() and end() member functions of STL containers are now magical? -- in the sense that the language knows about them. Or is something else going on?   ;Quote:Original post by alvaroOr you can use a loop...Sure, but it's often annoying to write out the iterator types for a loop.  Granted, he's using C++0x so he has auto, but anyway the point was just that it's not the fault of the lambda, it's the fault of for_each.   ;  zyrolasting: I see where your "for each" came from: Quote:wikipediaMicrosoft Visual C++ does support a nonstandard for each construct in managed and native code.Quote:Original post by jwezorekQuote:Original post by phresnelstd::find_if seems to be for you. Or if you need more fine-tuning, what about for (auto it : p->mEntitties) {...}?Off topic, but does the new for-each syntax mean that the begin() and end() member functions of STL containers are now magical? -- in the sense that the language knows about them. Or is something else going on?I think you can now call them magical, it struggles me too and does somehow not fit the world of C++, but on the other hand I wouldn't say nay to them, as it does not infer with the other C++ principles (e.g. does not cost you performance or so) ;)edit: not really magical, seems like there must be begin(T) and end(T) function for any T you want to loop over:Quote:6.5.4 The range-based for statement [stmt.ranged]The range-based for statement    for( for-range-declaration:expression ) statementis equivalent to    {   auto&&__range=( expression );   for(auto__begin= begin-expr,  __end= end-expr;  __begin!=__end;  ++__begin   ){  for-range-declaration =*__begin;  statement   }    }where __range, __begin, and __end are variables deﬁned for exposition only, and _RangeT is the type of the expression, and begin-expr and end-expr are determined as follows:.—if _RangeT is an array type, begin-expr and end-expr are __range and __range+__bound,respectively, where __bound is the array bound. If _RangeT is an array of unknown size or an array of incomplete type, the program is ill-formed.—otherwise, begin-expr and end-expr are begin(__range) and end(__range), respectively, where begin and end are looked up with argument-dependentlookup(3.4.2). For the purposes of this name lookup,namespace std is an associated namespace.[Example:int array[5]={1,2,3,4,5};for(int&x:array)    x*=2;—endexample ]Phew, had to re-format that whole paragraph, foxit does not properly paste :/edit: And for the begin()/end() functions which ship with C++:Quote:24.6.5 range access [iterator.range]1 In addition to being available via inclusion of the <iterator> header, the function templates in 24.6.5 areavailable when any of the following headers are included: <array>, <deque>, <forward_list>, <list>,<map>, <regex>, <set>, <string>, <unordered_map>, <unordered_set>, and <vector>.template <class C> auto begin(C& c) -> decltype(c.begin());template <class C> auto begin(const C& c) -> decltype(c.begin());Returns: c.begin().template <class C> auto end(C& c) -> decltype(c.end());template <class C> auto end(const C& c) -> decltype(c.end());Returns: c.end().template <class T, size_t N> T* begin(T (&array)[N]);Returns: array.template <class T, size_t N> T* end(T (&array)[N]);Returns: array + N.(okay, acrobat was nicer, only had to add code-tags)   