boost中的数值范围迭代器? [英] Numerical range iterators in boost?
问题描述
我知道boost中的范围迭代器,以及这个参考,似乎应该有一种简单的方法来做我想要的,但这对我来说并不明显。
I'm aware of the range iterators in boost, and as for this reference, it seems there should be an easy way of doing what I want, but it's not obvious to me.
说我想要表示数字范围,0到100(包括或不包括),例如范围(0,100)
。我想做类似的事情:
Say I want to represent a numerical range, 0 to 100 (inclusive or not), say range(0,100)
. I would like to do something like:
for_each(range<int>(0,100).begin(), range<int>(0,100).end(), do_something);
其中 do_something
是一个仿函数。这个迭代器不应该有一个下面的向量或类似的东西的开销,但只提供一个整数序列。这可能与boost中的范围实现有关吗?可以使用普通的标准STL迭代器吗?
where do_something
is a functor. This iterators shouldn't have the overhead of having an underneath vector or something like this, but to just offer a sequence of integers. Is this possible with the range implementation in boost? Possible at all with normal, standard STL iterators?
推荐答案
#include <boost/iterator/counting_iterator.hpp>
std::for_each( boost::counting_iterator<int>(0),
boost::counting_iterator<int>(100),
do_something );
这篇关于boost中的数值范围迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!