如何根据 set.begin() 的偏移量迭代 std::set?
How to iterate std::set based on offset from set.begin()?
我需要得到一个基于偏移量的迭代器。
即让迭代器开始 :
auto it = set.begin()
我需要到达具有偏移量 ofst
:
的迭代器
it + ofst
有办法吗?我需要增量增加 it++
迭代器 ofst
次。
I need to get to the iterator having offset ofst
: it + ofst
: is there
a way to do that?
不,没有为此定义 std::set::iterator
的 operator+
重载(又名 双向迭代器 )。但是,您可以使用 std::next
, from <iterator>
header 来实现相同的效果。
#include <iterator> // std::next
auto nthIter = std::next(it, ofst);
这基本上也在幕后增加了 ofst
倍。
std::set
有bidirectional iterators, which has no luxuries like random access iterators,因此需要这样递增。
也就是说,您可以为双向迭代器 which will not be recommended though.
重载 operator+
(也许 operator-
)
我需要得到一个基于偏移量的迭代器。
即让迭代器开始 :
auto it = set.begin()
我需要到达具有偏移量 ofst
:
it + ofst
有办法吗?我需要增量增加 it++
迭代器 ofst
次。
I need to get to the iterator having offset
ofst
:it + ofst
: is there a way to do that?
不,没有为此定义 std::set::iterator
的 operator+
重载(又名 双向迭代器 )。但是,您可以使用 std::next
, from <iterator>
header 来实现相同的效果。
#include <iterator> // std::next
auto nthIter = std::next(it, ofst);
这基本上也在幕后增加了 ofst
倍。
std::set
有bidirectional iterators, which has no luxuries like random access iterators,因此需要这样递增。
也就是说,您可以为双向迭代器 which will not be recommended though.
重载operator+
(也许 operator-
)