修改下属简短而丑陋的程序,使其使用lambda表达式而不是函数符Adder,请不要修改sum()。
#include <iostream> #include <array> const int Size = 5; template <typename T> void sum (std::array <double ,Size> a,T&fp); class Adder { double tot; public; Adder (double q = 0) :tot (q) {} void operator () (double w) {tot +=w;) double tot_v () const {return tot;}; }; int main() { double total = 0.0; Adder ad(total); std::array<double,Size>temp_c = {32.1,34.3,37.8,35.2,34.7} sum (temp_c,ad); total = ad.tot_v(); std::cout << "total:" << ad.tot_v() << '\n'; return 0; } template <typename T> void sum (std::array<double,Size>a,T& fp) { for (auto pt = a.begin();pt != a.end(); ++pt) { fp(*pt); } }