获取迭代器范围的每个第 n 个元素 [英] Get each nth element of iterator range

查看:22
本文介绍了获取迭代器范围的每个第 n 个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何标准实现(意味着 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.

这篇关于获取迭代器范围的每个第 n 个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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