If I have a range and I want to transform adjacent pairs is there a boost range adaptor to do this?
for example
std::vector<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
auto b = a
| boost::range::transformed([](int x, int y){return x+y;});
and the output would be
3, 5
EDIT
I have made an attempt at a range adaptor
// Base class for holders
template< class T >
struct holder
{
T val;
holder( T t ) : val(t)
{ }
};
// Transform Adjacent
template <typename BinaryFunction> struct transformed_adjacent_holder
: holder
{
transformed_adjacent_holder(BinaryFunction fn) : holder<BinaryFunction>(fn)
};
template <typename BinaryFunction> transform_adjacent
(BinaryFunction fn)
{ return transformed_adjacent_holder<BinaryFunction>(fn); }
template< class InputRng, typename BinFunc>
inline auto Foo(const InputRng& r, const transformed_adjacent_holder<BinFunc> & f)
-> boost::any_range
< std::result_of(BinFunc)
, boost::forward_traversal_tag
, int
, std::ptrdiff_t
>
{
typedef boost::range_value<InputRng>::type T;
T previous;
auto unary = [&](T const & t) {auto tmp = f.val(previous, t); previous = t; return tmp; };
return r | transformed(unary);
}
However I don't know how to deduce the return type of the | operator. If I can do this then the adaptor is almost solved.
boost::range::adjacent_differenced
, akin tostd::adjacent_difference
, like there isboost::range::transformed
(which is akin tostd::transform
) – Caleth