There are a lot of functions in the C++ standard library that take in an iterator pointing to the start of some collection, followed by an iterator pointing to the end of it. Quite often we want to operate on all the elements in a collection, rather than some subset, so we end up with a lot of repetition of .begin()
and .end()
. Is there a more concise way of writing this in C++?
For example:
std::vector<int> i = {1,2,3,4,5};
int sum = std::accumulate(i.begin(), i.end(), 0);
It would be nice if we could write something like std::accumulate(i, 0)
instead. Is there a way of doing something like this?