# need help about find() ??

## Recommended Posts

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 on other sites
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 on other sites
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 exampleprivate:	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 on other sites
Quote:
 Original post by rip-offTry 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 on other sites
Quote:
 Original post by Great_WhiteHow can I pass a parameter to the functor?

Use a constructor. (And post your error messages next time!)

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627646
• Total Posts
2978381

• 10
• 12
• 22
• 13
• 33