简体   繁体   English

查找列表的最左边和最右边的点。 std :: find_if正确的方法吗?

[英]Finding the left-most and right-most points of a list. std::find_if the right way to go?

I have a list of Point objects, (each one with x,y properties) and would like to find the left-most and right-most points. 我有一个Point对象的列表(每个对象都具有x,y属性),并且想找到最左边和最右边的点。 I've been trying to do it with find_if, but i'm not sure its the way to go, because i can't seem to pass a comparator instance. 我一直在尝试使用find_if进行操作,但是我不确定它的路要走,因为我似乎无法通过比较器实例。 Is find_if the way to go? 是find_if要走的路吗? Seems not. 好像没有。 So, is there an algorithm in <algorithm> to achieve this? 那么, <algorithm>是否有一种算法可以实现这一目标?

Thanks in advance. 提前致谢。

#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;
}

Use std::min_element and std::max_element instead. 请改用std::min_elementstd::max_element

list<Point>::iterator left = std::min_element(points.begin(), points.end(), left);
list<Point>::iterator right = std::max_element(points.begin(), points.end(), left);

I would also change the signature of left to: 我还将将left的签名更改为:

bool left(const Point& p1, const Point& p2)

If you use pair<float, float> instead of your own Point , there's no need for a special comparator. 如果使用pair<float, float>代替自己的Point ,则不需要特殊的比较器。 There would also be an ordering on the y-axis of points with the same x-coordinate, which can be useful. 在具有相同x坐标的点的y轴上也将有一个排序,这很有用。

There are various ways to imbue typedef pair<float, float> Point; 有多种方法可以使typedef pair<float, float> Point; with custom behavior, if you're so inclined. 具有自定义行为(如果您愿意)。 For example, 例如,

typedef pair<float, float> Point;

enum AxisUnit { x, y };
float &operator*( Point &p, AxisUnit c ) // "special case" of inner product
     { return c == x? p.first : p.second; }

Point my_point( 2.5, 6.3 );
float x_coord = my_point * x;

even better would be to use the boost minmax element: 更好的方法是使用boost minmax元素:

http://www.boost.org/doc/libs/1_42_0/libs/algorithm/minmax/index.html http://www.boost.org/doc/libs/1_42_0/libs/algorithm/minmax/index.html

#include <boost/algorithm/minmax_element.hpp>
...
auto res = boost::minmax_element(points.begin(), points.end(), left);

std::cout << "min: " << res.first << std::endl;
std::cout << "max: " << res.second << std::endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当选择最左边或最右边的元素作为枢轴时,快速排序显然采用O(n ^ 2) - Quick sort apparently taking O(n^2) when left or right-most element is selected as pivot 使用C ++和opencv查找最左,最右,最高和最向下的蓝色像素 - Finding the most left, most right, most high and most down blue pixel with C++ and opencv 在两个整数之间交换最右边的N位 - Swap right-most N bits between two integers 将SFINAE条件移至最左侧以便于阅读 - Move SFINAE condition to the left-most for readability 用于计算位或找到最右边|最左边的位的高效按位运算 - Efficient bitwise operations for counting bits or find the right|left most ones 从unsigned int(C ++)读取Left-Most位的最快方法? - Fastest way to read Left-Most bit from unsigned int (C++)? 除以“ 6”作为最右边数字的整数输入之外的任何整数输入均可,但是诸如…6之类的任何数字都会导致分段错误,为什么? - Any integer input except one with “6” as the right-most digit works, but any number like …6 leads to segmentation fault, why? C ++,std :: list的左/右旋转 - C++, left/right rotation of std::list 查找左上角和右下角点 (C++) - Finding Top Left and Bottom Right Points (C++) std :: vector和std :: list find_if和max_element性能 - std::vector and std::list find_if and max_element performance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM