algorithms/adapters/plus-example/plus-5.cpp - Yet another alternative where a lambda expression is used. | Lecture 6 - slide 25 : 40 Program 4 |
// We show how a lambda expression can be used instead. #include <iostream> #include <list> #include <numeric> // accumulate #include <functional> // binary_function int main(){ std::list<double> lst; for(int i = 1; i <= 10; i++) lst.push_back(i); // 1, 2, ..., 10 // We can also supply a lambda expression. double res = std::accumulate(lst.begin(), lst.end(), 0.0, [](const double& d, const double& e){return d + e;}); std::cout << "The plus accumulation is: " << res << std::endl; // 55 }