Boost logo

Boost Users :

Subject: Re: [Boost-users] Iterator Range, sub range of a desired size
From: Neil Groves (neil_at_[hidden])
Date: 2012-10-30 14:05:53


On Tue, Oct 30, 2012 at 4:50 PM, David Kimmel <davidwkimmel_at_[hidden]>wrote:

> I tried it out, yes you can get make_iterator_range with next to work. I
> like boost::next but for what I wanted it does not seem to work
> appropriately. Your example of "make_iterator_range(boost::next(b.begin,
> wSize), v.end())" will only skip the first "wSize" elements. What I want
> is a range that contains at most wSize elements (if possible) - taking them
> from after (and containing) a start index.
>
>
boost::next absolutely shouldn't be checking against end(), that would not
be obeying the zero-overhead principle. For your requirements surely you
just alter the parameters to make_iterator_range?

> Even my second attempt with this approach (commented out lines) does not
> work because next does not check to see if it is past end. Perhaps if
> there is a "min" method I could pass the "boost::next(v.begin() +
> (start-1), window)" and "v.end()".
>
>
std::min from <algorithm> ?

> typedef vector<int> Vector;
> Vector v = { 1, 2, 3, 4, 5, 6, 7, 8 , 9, 10, 11 };
>
> const int start = 10;
> const int window = 4;
>
> auto r = boost::make_iterator_range(boost::next(v.begin(), start-1),
> v.end());
> auto r2 = boost::make_iterator_range(boost::prior(r.end(), window),
> r.end());
>
> // auto r = boost::make_iterator_range(v.begin() + (start-1),
> boost::next(v.begin() + (start-1), window) );
> // auto r2 = boost::make_iterator_range(boost::prior(r.end(), window),
> r.end());
>
>

// General solution for all iterator types - assume that source here is v
from your example
auto r = boost::make_iterator_range(
    boost::next(boost::begin(source), start - 1),
    boost::next(boost::begin(source), std::min(start - 1 + window,
static_cast<int>(boost::distance(source))))
);

I'm not seeing a problem. Am I misunderstanding your question?

Neil Groves



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net