如何对段C 进行分类

How to sort the segments c++

本文关键字:分类      更新时间:2023-10-16

i具有边缘的向量。我需要按y坐标的Y级横穿垂直线x ==(a b(/2和那些边缘的点进行分类。问题在于A和B不是恒定的,他们必须从一个edgges变为另一个。如何将A和B参数发送到比较器?

struct vertex
{
   double x,y;
    bool operator==(const vertex &o)const {
        return x == o.x && y == o.y;
    }
    bool operator<(const vertex &o) const{
        return x < o.x || (x == o.x && y < o.y);
    }
};
typedef vector<vertex> vertList;
typedef vector <pair<vertex,vertex>> Edge;

您可以定义一个将ab作为构造函数参数的比较类别:

struct EdgeComparator {
    int a;
    int b;
    EdgeComparator(int a, int b): a(a), b(b) {}
    bool operator<(const Edge& lhs, const Edge& rhs) const {
        // You can compare lhs and rhs using a and b here
    }
};

然后以后将其实例传递给排序函数:

std::sort(v.begin(), v.end(), EdgeComparator(some_value_of_a, some_value_of_b));