简体   繁体   English

范围v3中的boost :: counting_iterator模拟

[英]boost::counting_iterator analogue in range-v3

I am wondering if range-v3 library has any view/utility providing the functionality similar to boost::counting_iterators ? 我想知道range-v3库是否具有任何视图/实用程序,提供的功能类似于boost :: counting_iterators吗?

I was looking for something of that kind, but seems there is nothing readily available. 我一直在寻找那种东西,但似乎没有现成的东西。 But the documentation is incomplete (at least the README suggests so) so maybe there is something I have overlooked. 但是文档不完整(至少自述文件建议如此),所以也许有些事情我被忽略了。

UPD: what I am actually looking for is not just a range of integers like view::iota but rather a range view (wrapper) that accepts any Incrementable. UPD:我实际上在寻找的不仅是像view :: iota这样的整数范围,而且还是一个接受任何Incrementable的范围视图(包装器)。 One such example is the following code from Boost.CountingIterator documentation: Boost.CountingIterator文档中的以下代码就是这样的示例:

int N = 7;
std::vector<int> numbers;
...
// the code below does what I am actually looking for
// I would like to use a single range instead of two separate iterators here
std::vector<std::vector<int>::iterator> pointers;
std::copy(boost::make_counting_iterator(numbers.begin()),
          boost::make_counting_iterator(numbers.end()),
          std::back_inserter(pointers));

You do, in fact, want view::iota . 实际上,您确实需要view::iota It accepts any WeaklyIncrementable type for the range iterator type (not only integral types), and any type that is WeaklyEqualityComparable to that type as the range sentinel. 它接受范围迭代器类型的任何WeaklyIncrementable类型(不仅是整数类型),还可以WeaklyEqualityComparable与该类型WeaklyEqualityComparable任何类型作为范围WeaklyEqualityComparable So view::iota(0, 8) is the range of integers {0,1,2,3,4,5,6,7} , and view::iota(i, s) is the range of iterators {i,i+1,i+2,...,s} . 所以view::iota(0, 8) {0,1,2,3,4,5,6,7} view::iota(0, 8)是整数{0,1,2,3,4,5,6,7}的范围,而view::iota(i, s)是迭代器{i,i+1,i+2,...,s}的范围{i,i+1,i+2,...,s} The boost counting_iterator example translates to range-v3 as ( DEMO ): boost counting_iterator示例将范围v3转换为( DEMO ):

int N = 7;
std::vector<int> numbers = ranges::view::iota(0, N);

std::vector<std::vector<int>::iterator> pointers =
    ranges::view::iota(numbers.begin(), numbers.end());

std::cout << "indirectly printing out the numbers from 0 to " << N << '\n';
ranges::copy(ranges::view::indirect(pointers),
    ranges::ostream_iterator<>(std::cout, " "));
std::cout << '\n';

I believe view::ints is what you're looking for. 我相信view::ints是您想要的。 You can make an unbounded range, and truncate it with something like view::take : 您可以创建一个无界范围,并使用诸如view::take类的方法截断它:

using namespace ranges;
int x = accumulate(view::ints(0) | view::take(10), 0); // 45

or you can make a bounded range (lower inclusive, upper exclusive) 或者您可以进行有界范围(低端包含,高端排除)

int x = accumulate(view::ints(0, 10), 0);

For your example, you can use view::iota instead. 对于您的示例,可以改用view::iota

copy(view::iota(numbers.begin(), numbers.end()), back_inserter(pointers));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM