Opened 8 years ago

Last modified 7 years ago

#10894 new Feature Requests

Add an adaptor to iterate a range in pairs of (current element, next element)

Reported by: Matthäus Brandl (brandl.matthaeus@… Owned by: Neil Groves
Milestone: To Be Determined Component: range
Version: Boost Development Trunk Severity: Not Applicable
Keywords: range adaptor pairwise Cc:

Description

An often needed functionality is iterating a range and accessing the current and next element of the range.

Currently this can be done as follows:

assert(someVec.size > 1);
for(auto const & pair, boost::make_iterator_range(
    boost::make_zip_iterator(
        boost::make_tuple(
            someVec.cbegin(), std::next(someVec.cbegin()))),
    boost::make_zip_iterator(
        boost::make_tuple(
            std::prev(someVec.cend()), someVec.cend())))
        )
{
    auto current = boost::get<0>(pair);
    auto next    = boost::get<1>(pair);
}

An adaptor would strongly increase readability:

assert(someVec.size > 1);
using boost::adaptors::operator|;
for(auto const & pair, someVec | boost::adaptors::paired())
{
    pair.current();
    pair.next();
}

Of course the difference between current and next could be made customizable as well.

Change History (1)

comment:1 by Matthäus Brandl, 7 years ago

This can also be achieved in a more concise way:

boost::combine(
    boost::make_iterator_range(c, 0, -1),
    boost::make_iterator_range(c, 1, 0))
Note: See TracTickets for help on using tickets.