Numerical range iterators in boost?(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 );
这篇关于boost中的数字范围迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:boost中的数字范围迭代器?
基础教程推荐
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01