获取迭代器范围的每个第 n 个元素

2022-01-10 00:00:00 iterator c++ boost std

是否有任何标准实现(意味着 stdlib 或 boost)的迭代器包装另一个迭代器并只给出它的每个第 n 个元素?

Is there any standard implementation (meaning stdlib or boost) of an iterator that wraps another iterator and gives only each nth element of it?

我最初认为这可以通过合适的谓词和 boost::filter_iterator 来实现,但是谓词只获取值而不是基迭代器,因此它无法判断到开始的距离.

I first thought this would be possible with a fitting predicate and boost::filter_iterator, but the predicate gets only the value and not the base iterator, so it cannot tell the distance to the start.

编辑
提供更多信息:迭代器应该与 std::transformstd::copy 等函数兼容.所以它应该像 stdlib 迭代器一样使用.

Edit
To give some more information: The iterator should be compatible with functions like std::transform or std::copy. So it should be used like stdlib iterators.

类似问题:
C++/STL: std::transform with given stride?
具有非随机访问迭代器的非单元迭代器步幅

推荐答案

Boost.Range 提供 一个步幅适配器.使用 boost::begin/boost::end 将为您提供相关的迭代器.

Boost.Range provides a stride adaptor. Using boost::begin/boost::end would net you the associated iterators.

相关文章