Revise the following short and ugly program so that it uses a lambda expression instead of the

Question:

Revise the following short and ugly program so that it uses a lambda expression instead of the Adder functor. Don’t change sum().
#include
#include
const int Size = 5;
template

void sum(std::array 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 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
void sum(std::array a, T& fp)
{
for(auto pt = a.begin(); pt != a.end(); ++pt)
{
fp(*pt);
}
}

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  answer-question

C++ Primer Plus

ISBN: 9780321776402

6th Edition

Authors: Stephen Prata

Question Posted: