Madz
Madz

Reputation: 1279

Find index range of specific values in sorted vector of vectors

I have a sorted std::vector<std::vector<double>> of x,y,z values as shown below,

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

I want to find all z values of a specific x and y value, ex - z values of 0.0, 0.0, should return

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

I tried using

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));

But this only gives me the iterator to a single vector value. What method is best suited to achieve this?

Thank you.

Upvotes: 0

Views: 788

Answers (1)

Mohit Jain
Mohit Jain

Reputation: 30489

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

This creates a new vector ret of the same type as allpoints and copy only the points of interest using copy_if

Upvotes: 2

Related Questions