我有一个 Point 对象列表(每个对象都有 x,y 属性),并且想找到最左边和最右边的点。我一直在尝试用 find_if 来做,但我不确定它要走的路,因为我似乎无法传递比较器实例。find_if 是要走的路吗?似乎没有。那么,是否有一种算法<algorithm>
可以实现这一点?
提前致谢。
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
typedef struct Point{
float x;
float y;
} Point;
bool left(Point& p1,Point& p2)
{
return p1.x < p2.x;
}
int main(){
Point p1 ={-1,0};
Point p2 ={1,0};
Point p3 ={5,0};
Point p4 ={7,0};
list <Point> points;
points.push_back(p1);
points.push_back(p2);
points.push_back(p3);
points.push_back(p4);
//Should return an interator to p1.
find_if(points.begin(),points.end(),left);
return 0;
}