C++ 二维数据的sort排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_27280587/article/details/82013217
struct Nodes
{
    int x, y;
};
Nodes nd[MAX_N];

int main(void){
    ......
    //按照y升序 x降序排列
    sort(nd, nd+ n,
         [](Nodes & n1, Nodes & n2)
    {
        return n1.y == n2.y ? n1.x > n2.x : n1.y < n2.y;
    });
}

或直接

struct node{
    int x, y;
    node(int x, int y):x(x), y(y){}
    node(){}
    bool operator<(const node& n) const{
        return this->y == n.y ? this->x > n.x : this->y < n.y;
    }
};

猜你喜欢

转载自blog.csdn.net/baidu_27280587/article/details/82013217