【问题标题】:Find index range of specific values in sorted vector of vectors在向量的排序向量中查找特定值的索引范围
【发布时间】:2015-02-25 10:49:38
【问题描述】:

我有一个 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));

但这只会给我一个指向单个向量值的迭代器。什么方法最适合实现这一目标?

谢谢。

【问题讨论】:

标签: c++ vector stl containers


【解决方案1】:
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 仅复制兴趣点

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2022-10-18
    • 2021-11-25
    相关资源
    最近更新 更多