Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          algorithms/adapters/plus-example/plus-3.cpp - A possible definition of plus.Lecture 6 - slide 25 : 40
Program 2

// 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 <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
}