using of std::accumulate

后端 未结 5 1346
面向向阳花
面向向阳花 2021-02-04 17:40

Need prettier solution of below example but with std::accumulate.

#include 
#include 
#include 

class Object
{
pu         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 18:40

    One would hope this is homework...

    struct Adapt { 
        static double mul(Object const &x) { return x.GetA() * x.GetB(); }
        static double operator()(Object const &x, Object const &y) { 
           return mul(x)+mul(y); } };
    

    and

    result = std::accumulate(collection.begin(), collection.end(), Object(0,0),Adapt() );
    

    assuming you're not allowed to touch the declaration of Object.

提交回复
热议问题