1.第一种情况(大根堆小于号,小根堆大于号)
用于priority_queue<int,vector<int>,cmp > heap;
struct cmp1
{
int x,int y;
bool operator()(int &a,int &b)//记忆方法:把b看成队首元素,因此小的在前面
{
return a>b;
}
};
priority_queue<int,vector<int>,cmp1 > heap 最小值优先:小根堆;
struct cmp2
{
int x,int y;
bool operator()(int &a,int &b)//把b看成队首元素,因此大的在前面
{
return a<b;
}
};
priority_queue<int,vector<int>,cmp1 > heap 最大值优先:大根堆;
2.第二种情况(只能重载小于号)
用于priority_queue<Rec> heap;(默认是大根堆)
struct Rec1
{
int x,y;
bool operator<(const Rec &t)const
{
return x<t.x
}
};
priority_queue<Rec1> heap; 重载小于号,大的在前面:大根堆
struct Rec2
{
int x,y;
bool operator<(const Rec &t)const
{
return x>t.x
}
};
priority_queue<Rec2> heap; 重载小于号,小的在前面:小根堆
3.第三种情况(只能重载大于号,不能重载小于号)
用于priority_queue<Rec,vector<Rec>,greater<Rec>> heap(默认是小根堆)
struct Rec1
{
int x,y;
bool operator>(const Rec1 &t)const
{
return x>t.x;
}
};
priority_queue<Rec1,vector<Rec1>,greater<Rec1> > heap 小根堆
struct Rec2
{
int x,y;
bool operator>(const Rec2 &t)const
{
return x<t.x;
}
};
priority_queue<Rec2,vector<Rec2>,greater<Rec2> > heap 大根堆
struct rec {
int x, y;//a[i][HTML_REMOVED] (rec a, rec b) {
return a.y < b.y;
}
在重载大于号时,为什么老是提示return __it1 < __it2;
应该怎么写重载大于号?
好
总结的好!
大佬你结构体下面少了个分号qaq
谢谢指正,已改
大根。。