I’ve a function taking a constant iterator in parameter and I would like to make a recursive call on the function, but incrementing my iterator by one (without modifying the iterator itself).
The only solution I found was to create a copy of the iterator, and pre-increment this copy to finally pass it as parameter of my function call.
func(const FowardIterator & i, const container & c) {
// lot of very smart code
FowardIterator next = i;
++next;
func(next, c);
}
Is there any simpler way to do this, one which does not involve creating a temporary iterator ?
An iterator should be extremely fast to copy, so there’s really no reason to accept one by
const&; pass-by-value should be fine, and would allow you to avoid copying the iterator explicitly in the body of the function.That said, you can also use
std::nextto copy an iterator and advance it all in one step: