【发布时间】:2010-04-16 08:31:56
【问题描述】:
我有一个 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;
}
【问题讨论】:
标签: c++ stl find comparator