algorithms/adapters/plus-example/plus-1.cpp - An illustration of plus<double>. | Lecture 6 - slide 25 : 40 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; // We make a list of doubles, and insert some numbers in the list: 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 }