Hi, so I had this exercise where I have to make a lot of implementations for something similar like accumulate(vec.begin, vec.end(), 0) and check for speed afterwards
so I wrote this mixing together lambda and for_each()
I'm wondering if this is the best way to write it if I want to use lambda + for_each().
I mean i looked at for_each() implementation and saw that it actually returns unary function that was passed to it as 3rd argument.
I saw in the example that its easy to create function object and pass it as predicate. Than when passing it as argument I can initialize it with 0 (starting sum) and it keeps track of this sum number until the object is returned and I can check the result.
Is there anyway I can do stuff like this with lambda if I don't want to use reference to outside variable (sum) to keep track of sum of all values? Or maybe some other way should be preferred?
Thank you very much for your response and all these examples man! I don't even understand some of the template stuff yet but looks like if I want to use lambda and for_each() I have to use [&sum].