1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <functional>
#include <algorithm>
#include <numeric> // <==== header
int sq(int c, int d) {return c+d*d;} // <==== binary op *******
int main () {
int numbers[]={10,20,30};
int result;
result = std::accumulate (numbers, numbers+3, 100, std::minus<int>());
std::cout << "The result of 100-10-20-30 is " << result << ".\n";
int sample[]={3 ,21 ,98 ,203 ,17 ,9};
result = std::accumulate (sample, sample+6, 0, sq);
std::cout << "The result of squares is " << result << ".\n";
return 0;
}
|