使用模板实现组合功能

时间:2010-03-26 19:06:43

标签: c++ templates

关于如何为模板做的任何想法?感谢

例如,Combine(<list containing 6,3,1,9,7>, std::plus<int>()) 应该计算((((6+3)+1)+9)+7)

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

1 个答案:

答案 0 :(得分:2)

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

(错误处理留作OP和读者的练习。)