在向量的排序向量中查找特定值的索引范围

Find index range of specific values in sorted vector of vectors

本文关键字:向量 索引 范围 查找 排序      更新时间:2023-10-16

我有一个x、y、z值的排序std::vector<std::vector<double>>,如下所示,

0.0, 0.0, 0.0
0.0, 0.0, 0.1
0.0, 0.0, 0.2
0.0, 0.1, 0.0
0.0, 0.2, 0.1
0.0, 0.2, 0.3

我想找到特定x和y值的所有z值,ex-z值0.0,0.0,应返回

0.0, 0.0, 0.0
0.0, 0.0, 0.1
0.0, 0.0, 0.2

我试过使用

struct MatchDouble
{
    MatchDouble(double _x,double  _y) : x(_x), y(_y) {}
    bool operator()(std::vector<double> &n)
    {
        return fabs(x - n[0]) < FLT_EPSILON && fabs(y - n[1]) < FLT_EPSILON;
    }
private:
    double x, y ;
};
it = find_if(allpoints.begin(), allpoints.end(),MatchDouble(0.0,0.0));

但这只为我提供了一个向量值的迭代器。什么方法最适合实现这一点?

谢谢。

std::vector<std::vector<double>> ret;
std::copy_if(allpoints.begin(), allpoints.end(), std::back_inserter(ret), MatchDouble(0.0,0.0));
return ret;

这将创建一个与allpoints类型相同的新vector ret,并使用copy_if 仅复制兴趣点