STL – put structure data into set

Time:2022-5-19

Set has natural sorting and de duplication functions. The structure is not a basic type (the basic type has a default sorting criterion), so the < operator needs to be overloaded. (equivalent to a sorting criterion for user-defined types)

Struct overload < operator
bool operator < (const node &other) const{
        if(x == other.x) return y < other.y;
        return x < other.x;
    }
#include 
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()
{
    setst;
    
    return 0;
}