| An illustration of plus<double>. | Lecture 5 - slide 39 : 39 Program 1 |
// A program that illustrate the difference between the plus operator
// and a function of two arguments that adds the arguments together.
#include <iostream>
#include <list>
#include <numeric> // accumulate
#include <functional> // plus
int main(){
using namespace std;
list<double> lst;
for(int i = 1; i <= 10; i++) lst.push_back(i); // 1, 2, ..., 10
// An operator is NOT a first class entity in C++:
double res = accumulate(lst.begin(), lst.end(), 0.0, +); // error: expected primary-expression ....
// We must make an object which serves as a binary plus function:
double res = accumulate(lst.begin(), lst.end(), 0.0, plus<double>() );
cout << "The plus accumulation is: " << res << endl; // 55
}