Avoid comparing invalidated iterators.