how do you reliably clamp an iterator?

if (myVector.end() - it < variable_amount)
    it += variable_amount;

Using std::distance and std::advance would allow this to work with non-random access iterators, but that would be an inefficient algorithm for them, so I recommend not using them.

Algorithm for non-random-access iterators:

for (; variable_amount-- && it != myList.end(); ++it);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top