2
std::accumulate
文檔指出:的std ::積累BinaryOperator副作用
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的這個實現?
'[&VEC](INT A,INT B){vec.clear();返回a + b; }'? – LogicStuff