编写模式识别'函数以查找特定顺序出现的数字?C++

writing a pattern recognition 'function to find occurrence of numbers in specific order? c++

本文关键字:定顺序 数字 C++ 查找 模式识别 函数      更新时间:2023-10-16

>我有一个容器,它存储来自源的双精度值,当数字以特定顺序/模式出现时,我需要找到数字的位置。

vector<double> m = {-0.15,0.2,-0.2,-0.1,0.5,-0.15,-0.8,0.35,-0.2,-0.35......nth}

有没有办法找到数字的位置,如果按顺序(按顺序(出现,例如:

m[x] = 0.1 to 0.5  //value of m[x] must between those two values
m[x+1] = 0.35 to 0.7 //anywhere between the range 
m[x+2] = -0.1 to 0.1 //         "     
m[x+3] = 0.0 to.03    //        "
std::search自定义

谓词。

您的模式将包含范围,如果 LHS 双精度位于 RHS 范围内,则需要一个返回 true 的自定义二进制谓词。

未经测试的示例:

using Range = std::pair<double,double>;
std::vector<Range> pattern {{0.1, 0.5}, {0.35, 0.7}, {-0.1, 0.1}, {0.0, 0.03}};
auto match = std::search(begin(m), end(m),
                         begin(pattern), end(pattern),
                         [](double d, Range r) {
                           return (r.first < d) && (d < r.second);
                         });

为您的双重比较等添加适当的 epsilon 等。