// We show a possible definition of plus, as a struct with an // application operator. The binary_function is not strictly necessary. // Notice that the struct plus is derived from binary_function (inheritance). // binary_function only contains a few typedefs. #include #include #include // accumulate #include // binary_function template struct plus: public std::binary_function { T operator() (const T& x, const T& y) const {return x+y;} }; int main(){ std::list 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{} ); std::cout << "The plus accumulation is: " << res << std::endl; // 55 }