结构体内嵌函数的使用
参考:https://blog.csdn.net/weixin_43735161/article/details/101349153
https://www.acwing.com/blog/content/352/
1.结构体内嵌函数
基本用法
struct node
{
int l, r;
bool operator< (const node &v)const
{
return r < v.r;
}
} k[maxn];
2. 普通结构体 sort+cmp用法
当我们定义了一个结构体数组,想让它从小到大排序时,需要多定义一个bool 类型的cmp函数
struct Node
{
int a,b;
} q[100];
bool cmp(Node x,Node y)
{
return x.a<y.a;
}
//当需要排序时
sort(q,q+100,cmp);
3.结合优先队列使用结构体内嵌函数
//升序队列
priority_queue <int,vector<int>,greater<int> > q;
//降序队列
priority_queue <int,vector<int>,less<int> >q;
注意 greater 和 less
而上面的升降序是针对 单个int型 元素的,如果元素是结构体,我们需要内嵌比较函数
struct Node{
int a, b;
bool operator &l