tags:

views:

42

answers:

1

any idea on how to do it for template? thanks

For example, Combine(<list containing 6,3,1,9,7>, std::plus<int>()) should calculate ((((6+3)+1)+9)+7).

Combine(const Container& c, Function fn) throw (NotEnoughElements)
{
   your code goes here
}
+2  A: 

returnstd::accumulate(c.begin()+1, c.end(), *(c.begin()), fn);

(Error handling is left as exercise for OP and reader.)

KennyTM
Thanks! How do we handle overflow (say large number sum) and overloading (say char type)? sry im not so experienced in template
@gkid123: `std::plus` cannot handle overflow. You need to write your own safe "plus" struct. Overloading is automatic.
KennyTM