优先队列
- empty( ) //判断一个队列是否为空
- pop( ) //删除队顶元素
- push( ) //加入一个元素
- size( ) //返回优先队列中拥有的元素个数
- top( ) //返回优先队列的队顶元素
排序
//默认从大到小排序,整数中元素大的优先级高
priority_queue<int> q1;
//从小到大排序
priority_queue<int,vector<int>,greater<int> >q2;
//自定义排序规则
int tmp[100];
struct cmp1{
bool operator()(int x,int y)
{
return x>y;//小的优先级高 ,从小到大排
}
};
struct cmp2{
bool operator()(const int x,const int y)
{
return tmp[x]>tmp[y];
}
};
struct node{
int x,y;
friend bool operator<(node a,node b)
{
return a.x>b.x;//按x从小到大排
}
};
priority_queue<int>q1;
priority_queue<int,vector<int>,cmp1>q2;
priority_queue<int,vector<int>,cmp2>q3;
priority_queue<node>q4;
实例1
struct node
{
int weight;
char ch;
node *left;
node *right;
string Huffcode;
node():left(NULL),right(NULL),ch('\0'),weight(0),Huffcode(""){}
};
struct cmp
{
bool operator () (node *a,node *b){
return a->weight>b->weight;
}
};
priority_queue<node*,vector<node*>,cmp> pque;