algorithms/adapters/plus-example/plus-4.cpp - Another definition of plus - as a function template. | Lecture 6 - slide 25 : 40 Program 3 |
// We show another definition of plus, as a function template. // A rather direct and more down-to-earth approach. #include <iostream> #include <list> #include <numeric> // accumulate #include <functional> // binary_function template<typename T> T plus_fn(T a, T b){ return a + b; } int main(){ std::list<double> lst; for(int i = 1; i <= 10; i++) lst.push_back(i); // 1, 2, ..., 10 // We must make an object which serves as a binary plus function: double res = std::accumulate(lst.begin(), lst.end(), 0.0, plus_fn<double>); std::cout << "The plus accumulation is: " << res << std::endl; // 55 }