cpp/iterator/counted iterator

is an iterator adaptor which behaves exactly like the underlying iterator, except that it keeps track of the distance to the end of its range. This iterator is equal to std if and only if its count reaches zero.