STL - 将结构体数据放入set中

Set具有天然的排序与去重功能,结构体不是基本类型(基本类型有默认的排序准则),因此需要重载 < 运算符。(相当于给自定义类型一个排序准则)

结构体重载 < 运算符
bool operator < (const node &other) const{
        if(x == other.x) return y < other.y;
        return x < other.x;
    }
#include <bits/stdc++.h>
using namespace std;

typedef struct node{
    int x,y;
    bool operator < (const node &other) const{
        if(x == other.x) return y < other.y;
        return x < other.x;
    }
}nd;

int main()
{
    set<nd>st;
    
    return 0;
}
posted @ 2022-04-08 16:33  gverzh  阅读(188)  评论(0)    收藏  举报