From the course: Introducing Functional Programming in C++

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

accumulate instead of reduce

accumulate instead of reduce - C++ Tutorial

From the course: Introducing Functional Programming in C++

Start my 1-month free trial

accumulate instead of reduce

- [Instructor] At last we've reached the Accumulate function. So here we will do int results equals accumulate and accumulate takes four parameters. It's going to take a filtered collection dot begin that is the begin iterator, a filtered collection dot end for end iterator, do a comma and then go to the next line. Then it takes a, and in this case it's going to be an integer value, which is the initial value for the accumulate. And in our case, it's going to be zero 'cause we want to add all our numbers together and we're going to start at zero. Then it takes the lambda function, which is a binary operation. And so this takes two values, the first one's going to be int total and the second one we're going to call int current. Then inside we're going to say return total plus current. And then a semi-colon, end of our curly brace, and then we're going to have a semi-colon. And right here we'll do a c out. And we'll call this accumulate and an end l, then we'll do another c out. And…

Contents