结构体运算符重载

1.降序

struct Point{
    int x, y;
    //重载比较符
    bool operator < (const Point &a) const{
        return x > a.x;//当前元素大时,是降序
    }
};

结构体运算符重载_第1张图片

2.升序

struct Point{
    int x, y;
    //重载比较符
//    bool operator < (const Point &a) const{
//        return x > a.x;//当前元素大,降序
//    }
    bool operator < (const Point &a) const{
        return x < a.x;//当前元素小,升序
    }
};

结构体运算符重载_第2张图片

你可能感兴趣的:(c++)