如何检索连接范围的 lambda 中的索引?

How to retrieve an index within a lambda for a concatenated range?

我有以下代码:

#include <range/v3/all.hpp>
#include <deque>
#include <iostream>

auto main() -> int
{
    using namespace ranges;
    namespace views = ranges::views;

    auto v1 = std::deque<double>({ 123.080, 123.110, 123.105, 123.090, 123.095 });
    auto v2 = std::deque<double>({ 123.100, 123.120, 123.115, 123.070, 123.105 });

    auto vc = ranges::views::concat(v1, v2); 
    
    auto lambda = [&](auto& elem){
        const auto index = &elem - &vc[0];
        if (index == 0 || index + 1 > vc.size()) return false;
        if ((vc[index - 1] > vc[index] && vc[index] < vc[index + 1])
            || (vc[index - 1] < vc[index] && vc[index] > vc[index + 1]))      
            return true;
    };

    //auto pv = vc | views::transform(lambda);
    auto pv = vc | views::filter(lambda);

    for(auto p : pv)
    {
        std::cout << p << ", ";
    }

    return 0;
}

但是 const auto index = &elem - &vc[0]; 语句导致了问题,因为 concat 不是连续的内存区域。它给出了以下结果:

123.11, 123.09,

什么时候应该给出:

123.110, 123.090, 123.120, 123.070,

这些过滤值形成了两个向量的连接视图的峰 (vc[index-1] < vc[index] && vc[index] > vc[index+1]) 和谷 (vc[index-1] > vc[index] && vc[index] < vc[index+1]),从而产生了一个单独的视图 (pv)原来的vc.

请问如何在与 ranges::concat 视图兼容的 lambda 表达式中创建索引?或者,有没有更好的方法来做到这一点 ranges?

提供直播区:here

全局索引好像走错了。

我会做类似的事情:

auto is_extrema = [](auto r3){
    return r3[0] < r3[1] && r3[1] > r3[2]
        || r3[0] > r3[1] && r3[1] < r3[2];
};
auto pv = vc | views::sliding(3) | views::filter(is_extrema);

Demo