| A possible definition of plus. | Lecture 5 - slide 39 : 39 Program 2 |
// We show a possible definition of plus, as a struct with an
// application operator. The binary_function is not strictly necessary.
// It only contains a few typedefs
#include <iostream>
#include <list>
#include <numeric> // accumulate
#include <functional> // binary_function
template <class T> struct plus: public std::binary_function<T,T,T> {
T operator() (const T& x, const T& y) const {return x+y;}
};
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<double>() );
std::cout << "The plus accumulation is: " << res << std::endl; // 55
}