以set具体说明
#include#includeusing namespace std;
struct node
{int x;
int y;
bool operator<(const node& a) const
{if (a.x == x)
return y< a.y;
return x< a.x;
}
};
//在 set 里面进行重载,和优先队列一样
void run_case()
{sets;
s.insert(node{1, 1 });
s.insert(node{1, 1 });
s.insert(node{1, 2 });
s.insert(node{1, 3 });
s.insert(node{2, 2 });
s.insert(node{2, 3 });
cout<< s.begin()->x<< " "<< s.begin()->y<< "\n"; // 输出 1,1
}
int main()
{run_case();
return 0;
}
方法二(一般推荐)#include#includeusing namespace std;
struct node
{int x;
int y;
};
class comp
{public:
bool operator()(const node& a, const node& b) const
{if (a.x == b.x)
return a.y< b.y;
return a.x< b.x;
}
};
void run_case()
{sets;
s.insert(node{1, 1 });
s.insert(node{1, 1 });
s.insert(node{1, 2 });
s.insert(node{1, 3 });
s.insert(node{2, 2 });
s.insert(node{2, 3 });
cout<< s.begin()->x<< " "<< s.begin()->y<< "\n";
}
int main()
{run_case();
return 0;
}
方法三(了解就好)#include#includeusing namespace std;
struct node
{int x;
int y;
};
bool comp(const node& a, const node& b)
{if (a.x == b.x)
return a.y< b.y;
return a.x< b.x;
}
void run_case()
{sets(comp);
s.insert(node{1, 1 });
s.insert(node{1, 1 });
s.insert(node{1, 2 });
s.insert(node{1, 3 });
s.insert(node{2, 2 });
s.insert(node{2, 3 });
cout<< s.begin()->x<< " "<< s.begin()->y<< "\n";
}
int main()
{run_case();
return 0;
}
对于multiset也是一样的用法,可以自行尝试
参考资料点这里
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
新闻标题:c++STL模板库的set和multiset自定义排序函数-创新互联
文章转载:https://www.cdcxhl.com/article16/dehgdg.html
成都网站建设公司_创新互联,为您提供网站维护、品牌网站制作、微信小程序、网站制作、响应式网站、网站建设
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联