boost中的数字范围迭代器?

2022-01-24 00:00:00 range iterator c++ stl boost

我知道 boost 中的范围迭代器,对于 this reference,似乎应该有一种简单的方法来做我想做的事,但这对我来说并不明显.

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(包括或不包括),比如 range(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?

推荐答案

boost::counting_iterator

#include <boost/iterator/counting_iterator.hpp>

std::for_each( boost::counting_iterator<int>(0),
               boost::counting_iterator<int>(100),
               do_something );

相关文章