1

std::adjacent_find looks for the first two consecutive elements that satisfies the given predicate. I am looking for other algorithms that also has a predicate that takes the (previous, current) pair. This is useful for rendering polygons, computing numerical quadrature based on sample data etc.

In addition to std::adjacent_find there is std::adjacent_difference, which can be used to implement a one-dimensional filter, but is not useful if you only want the sum of op(previous, current).

Can such functions be emulated with some kind of view from std::ranges?

user877329
  • 6,717
  • 8
  • 46
  • 88

1 Answers1

3

There's no special function for this, because you can just call the binary transform std::transform(c.begin(), std::prev(c.end()), std::next(c.begin()), op). This works on the overlapping ranges [c.begin(), c.end()-1] and [c.begin()+1, c.end()].

Similarly, other operations that take two input ranges can work on two overlapping ranges as long as they don't modify the inputs.

This can also work with greater differences than +/-1. It should be noted that this does not work on input iterators - they're one-pass.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • @user877329: For readability, I'd wrap the `transform` call together with the binary `op` in its own function. This will be a short function, of course, so it's implementation is readable enough. And elsewhere, you use the function name, so the particular implementation doesn't matter. – MSalters Jun 24 '21 at 13:52
  • @user877329 - for readability you wrap the one-line transform call in a little free function with _a proper name_, taking the distance and the transform lambda/function as parameters, and of course also your collections pair of iterators, you test it, then you use it however you want and forget how its done. – davidbak Jun 24 '21 at 13:53