[英]Numerical range iterators in boost?
我知道boost中的范围迭代器,对于本参考而言 ,似乎应该有一种简单的方法来完成我想要的操作,但这对我来说并不明显。
说我想代表一个数字范围,0到100(包括或不包括),例如range(0,100)
。 我想做类似的事情:
for_each(range<int>(0,100).begin(), range<int>(0,100).end(), do_something);
其中do_something
是一个函子。 该迭代器不应该拥有向量或类似内容的开销,而只需提供整数序列即可。 使用boost实现范围可能吗? 普通的标准STL迭代器完全可能吗?
#include <boost/iterator/counting_iterator.hpp>
std::for_each( boost::counting_iterator<int>(0),
boost::counting_iterator<int>(100),
do_something );
如果您从C ++ 11的角度出发,只是要添加其他答案-如果您更愿意使用现代的for-each循环,则可以使用boost count_range更干净地做到这一点:
#include <boost/range/counting_range.hpp>
for(auto const &i : boost::counting_range(0, 10)) {
std::cout << i;
}
输出:
0123456789
对的,这是可能的。 似乎boost :: range没有开箱即用的支持,但是您可以
boost::counting_iterator
,它就是您想要的 operator*()
将返回一个数字,并将其用作range
的迭代器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.