I know std::forward_list<T>::iterator
doesn't have a compound-assignment operator (operator+=
). But why is that?
I'm asking this for three reasons:
operator++()
? std::advance()
that does the same thing ?operator+=()
.Use:
std::advance(it, n);
(Declared in <iterator>
.)
The point is that compound-assignment operators are only provided when the operation has O(1) cost. Since incrementing a forward iterator has linear cost, it's better to make this explicit.
If you want a new value that's the result of repeated increments, use:
auto it2 = std::next(it1, n);