c++ - How do I return from a function inside a lambda? -
To determine whether there is an element in a category, consider the following toy code:
template & lt; Tyname Iter, Typing Named ATC; Bool contains1 (Iter start, Iter end, const T & x) {for (start! = End; ++ start) {if (* begin == x) true; } return false; } (Yes, I know, the standard library already has fine algorithms, it is not.)
How do I for_each and lambda? The following does not work ... template & lt; Typename iter, typename T & gt; Bool contains2 (Iter start, Iter end, const T & x) {std :: for_each (start, end, [& amp; x] (CONST T & amp;); if (x == y) return true;} ); return false; } ... because it will only return from Lambda, not from the function.
Do I have to throw an exception to get out of Lambda? Then, maybe this might be a dozen better solutions to this specific problem, which does not include Lambadas at all, but this is not what I am asking for.
How do I write the same thing with for_each and lambda ? Can you do? ??? t (except for one exception) Your function is not isomorphic for each loop (= the type of a mapping) It is as simple as it is. Instead, your function is described as reduced, so if you want to use high-order functions to change it, do not make a map, less Do it .
If was C ++ a suitable, normal - you decrease then your algorithm can See: template Bool contains2 (Iter start, Iter end, const T & x) {return stdx :: less (start, end) , [& Amp; x] (constant t & amp; y, bool accumulator) {return accumulator or x == y;});} Of course, it just exits quickly If there is a particular shortage for the short-circuit, especially for Boolean result values.
Hey, C ++ does not ?? As far as I'm watching, I do not provide such a facility. There is but short-circuit (can not do this) C ++ does not know that the operation inside is a lumbda short-circuit, and it is not implemented in an application form).
Comments
Post a Comment