【发布时间】:2016-07-28 18:11:49
【问题描述】:
std::accumulate cppreference.com 上的文档指出:
op 不能使任何迭代器失效,包括结束迭代器,或者 修改所涉及范围的任何元素(c++11 起)
稍后,它显示了一个可能的实现,我在这里报告:
template<class InputIt, class T, class BinaryOperation>
T accumulate(InputIt first, InputIt last, T init,
BinaryOperation op)
{
for (; first != last; ++first) {
init = op(init, *first);
}
return init;
}
假设 std::accumulate 的这种实现,op 如何“使某些迭代器无效”或“修改范围内的元素”?
【问题讨论】:
-
[&vec](int a, int b) { vec.clear(); return a + b; }?