如何将迭代器增加 2?

2021-12-22 00:00:00 iterator visual-c++ c++ stl

谁能告诉我如何将迭代器增加 2?

Can anybody tell me how to increment the iterator by 2?

iter++ 可用 - 我必须做 iter+2 吗?我怎样才能做到这一点?

iter++ is available - do I have to do iter+2? How can I achieve this?

推荐答案

std::advance( iter, 2 );

此方法适用于不是随机访问迭代器的迭代器,但在与随机访问迭代器一起使用时,它仍然可以通过实现进行专门化,使其效率不低于 iter += 2.

This method will work for iterators that are not random-access iterators but it can still be specialized by the implementation to be no less efficient than iter += 2 when used with random-access iterators.

相关文章