Boost logo

Boost :

From: Walker, Daniel (Daniel.Walker_at_[hidden])
Date: 2003-11-25 16:14:22


I'm new to the list, but I was just thing about the utilities next() and
prior(), and I have a suggestion. I think it would be very useful to
have a next_n() and prior_n() to simulate operators + and - for non
random access iterators. Often, I find myself in the situation where I
know the length of a sequence, but don't have the last iterator. For
example, I'd like to process all subsequences of length three. This is
fairly simple with random access iterators ...

    for(RandomAccessIterator i = first; i != last - 2; ++i)
        RandomAccessIterator my_first = i, my_last = i + 3;
        // ... do something

But with bidirectional iterators ...

    BidirectionalIterator tmp_last = last;
    std::advance(tmp_last, -2);
    for(BidirectionalIterator i = first; i != tmp_last; ++i)
        BidirectionalIterator my_first = i, my_last = i;
        std::advance(my_last, 3);
        // ... do something

What I'd like to be able to say is something like ...

    for(BidirectionalIterator i = first; i != boost::prior_n(last, 2);
        BidirectionalIterator my_first = i, my_last = boost::next_n(i,
        // ... do something

The implementation would be straightforward.

template <class T, class Size>
inline T next_n(T x, Size n)
    std::advance(x, n);
    return x;

template <class T, class Size>
inline T prior_n(T x, Size n)
    std::advance(x, -n);
    return x;

Is there another way that I'm not aware of that would give me a similar
effect? If not, is there any interest in next_n() and prior_n()?

Daniel Walker, Software Engineer
Bowne Global Solutions

Office 5095 Murphy Canyon Road
        San Diego, CA 92101 USA
Phone +1 858 737 5247
Mobile +1 619 251 4068
Fax +1 858 737 5297

Boost list run by bdawes at, gregod at, cpdaniel at, john at