Sign in to follow this  
Great_White

need help about find() ??

Recommended Posts

Great_White    148
Hi guys, I'm trying to search a vector of points(i.e. vector<point>plist ) with the x element of a point equals to 0.5. How can use the find() function on this vector? Here is my code:
struct point 
{
  point(int _X, int _Y): x(_X), y(_Y) { } 
  int x;
  int y;
};

// vector <point> plist;
// I am tring to find the x coodinate of points which is equal to 0.5
// find(plist.begin(), plist.end(), 0.5) ---> gives error


Share this post


Link to post
Share on other sites
rip-off    10976
Try std::find_if. You need to supply a functor (struct or class instance with operator() or a function pointer). The functor it should take a point and return a boolean.

Share this post


Link to post
Share on other sites
MaulingMonkey    1728
Errors are meant to be read, providing the message your compiler gives you will prevent others from having to compile your code to get them.

In this case, you're trying to find a point equal to 0.5. Let me state the number of ways this is wrong:

1) Points aren't numbers -- and you never tell std::find you're trying to find a point with it's X equal to.... This is what your compiler is complaining about when it complains about operator== -- it can't find one that compares a point to a value.
2) Floating point numbers are inexact. It's almost certainly a bug to try and find an element with an exact floating point value. Use a (small) range instead.
3) Since your point uses int x/y, neither of these can ever be 0.5 anyways.
4) std::find only works for finding values that are equal -- where "value" in this case would be "entire points". Anything else will need to use std::find_if.

Here's a version with the problems dealt with:
#include <algorithm>
#include <functional>
#include <vector>
#include <cmath>

struct point
{
point( double x, double y ): x(x), y(y) {}

double x;
double y;
};

struct has_x_near {
has_x_near( double x ): x(x) {}

bool operator()( const point& p ) const { return std::abs(p.x-x) < 0.001; } // crude example
private:
double x;
};

int main() {
std::vector<point> plist;
std::vector<point>::iterator item = std::find_if(plist.begin(), plist.end(), has_x_near(0.5) );
assert( item == plist.end() ); // since the list is empty, item should == .end() to indicate no such point was found
}


Share this post


Link to post
Share on other sites
Great_White    148
Quote:
Original post by rip-off
Try std::find_if. You need to supply a functor (struct or class instance with operator() or a function pointer). The functor it should take a point and return a boolean.


ok here is what I came up :


class FinderFunc {
public:
bool operator() (const point &p, int a) const {
return ( p.x == a);
}
};

find_if(A.begin(), A.end(), FinderFunc(0.5) ); // ---> this again gives error.



This again gives an error at the find_if function. It does not accept the parameter - 0.5 . How can I pass a parameter to the functor?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this