#include <iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct cmp{
bool operator()(const int a,const int b)
{
return a<b;
}
};
int main()
{
priority_queue<int,vector<int>,cmp >q;
q.push(5);
q.push(3);
q.push(19);
int c;
while(!q.empty())
{
c=q.top();
q.pop();
cout<<c<<endl;
}
return 0;
}
在priority_queue中cmp要在struct结构体中定义,且return a<b;是降序,反之是升序。
```go
#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef pair<int,int> P;
bool judge(const P a,const P b)
{
if(a.first==b.first)
return a.second<b.second;
else
return a.first>b.first;
}
int main()
{
vector<P>vect;
vect.push_back(P(4,5));
vect.push_back(P(4,1));
vect.push_back(P(14,5));
vect.push_back(P(10,7));
sort(vect.begin(),vect.end(),judge);
vector<P>::iterator it=vect.begin();
for(;it!=vect.end();it++)
cout<<it->first<<" "<<it->second<<endl;
return 0;
}
在sort中return a.first>b.first;是降序,与priority_queue不同。