Understanding std::accumulate

前端 未结 5 2031
轮回少年
轮回少年 2020-12-23 11:11

I want to know why std::accumulate (aka reduce) 3rd parameter is needed. For those who do not know what accumulate is, it\'s used like so:

5条回答
  •  春和景丽
    2020-12-23 11:42

    Because standard library algorithms are supposed to work for arbitrary ranges of (compatible) iterators. So the first argument to accumulate doesn't have to be begin(), it could be any iterator between begin() and one before end(). It could also be using reverse iterators.

    The whole idea is to decouple algorithms from data. Your suggestion, if I understand it correctly, requires a certain structure in the data.

提交回复
热议问题