迭代器的继任者

fre*_*low 7 c++ boost iterator stl

我想用另一个迭代器(同类)的后继者初始化一个迭代器(任意类型).以下代码适用于随机访问迭代器,但它使用正向或双向迭代器失败:

Iterator i = j + 1;
Run Code Online (Sandbox Code Playgroud)

一个简单的解决方法是:

Iterator i = j;
++i;
Run Code Online (Sandbox Code Playgroud)

但这并不适用于for循环的初始化.我可以使用如下的函数模板:

template <typename Iterator>
Iterator succ(Iterator it)
{
    return ++it;
}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

Iterator i = succ(j);
Run Code Online (Sandbox Code Playgroud)

在STL或Boost中有类似的东西,还是有更好的解决方案我不知道?

Fre*_*son 14

我想你正在寻找nextBoost.Utility.它还prior用于获取前一个元素的迭代器.

更新:

C++ 11介绍std::nextstd::prev.