迭代器的继任者 [英] iterator successor

查看:113
本文介绍了迭代器的继任者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要初始化(任意一种)与另一个迭代器的继任者(同类)的迭代器。下面code工作与随机访问迭代器,但它失败,向前或双向迭代器:

I want to initialize an iterator (of arbitrary kind) with the successor of another iterator (of the same kind). The following code works with random access iterators, but it fails with forward or bidirectional iterators:

Iterator i = j + 1;

有一个简单的解决方法是:

A simple workaround is:

Iterator i = j;
++i;

但是,这并不为的的init-stament for循环的工作的的。我可以用一个函数模板如下所示:

But that does not work as the init-stament of a for loop. I could use a function template like the following:

template <typename Iterator>
Iterator succ(Iterator it)
{
    return ++it;
}

,然后用它是这样的:

and then use it like this:

Iterator i = succ(j);

有没有这样的事情在STL或升压,或者是有一个更好的解决方案,我不知道呢?

Is there anything like that in the STL or Boost, or is there an even better solution I am not aware of?

推荐答案

我认为你正在寻找的 接下来在Boost.Utility 。它还具有之前获得一个迭代器到previous元素。

I think you're looking for next in Boost.Utility. It also has prior for obtaining an iterator to a previous element.

这篇关于迭代器的继任者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆