need help about find() ??

Started by
3 comments, last by MaulingMonkey 15 years, 11 months ago
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


"A human being is a part of a whole, called by us,universe, a part limited in time and space. He experiences himself, his thoughts and feelings as something separated from the rest... a kind of optical delusion of his consciousness. This delusion is a kind of prison for us, restricting us to our personal desires and to affection for a few persons nearest to us. Our task must be to free ourselves from this prison by widening our circle of compassion to embrace all living creatures and the whole of nature in its beauty."A. Einstein
Advertisement
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.
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}
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?
"A human being is a part of a whole, called by us,universe, a part limited in time and space. He experiences himself, his thoughts and feelings as something separated from the rest... a kind of optical delusion of his consciousness. This delusion is a kind of prison for us, restricting us to our personal desires and to affection for a few persons nearest to us. Our task must be to free ourselves from this prison by widening our circle of compassion to embrace all living creatures and the whole of nature in its beauty."A. Einstein
Quote:Original post by Great_White
How can I pass a parameter to the functor?

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

This topic is closed to new replies.

Advertisement