(https://www.cnblogs.com/icfnight/p/3239097.html)
(https://www.cnblogs.com/stranger-/p/7160577.html)
例如,实现一个“个位数大的整数优先级反而越小”的优先队列,可以定义一个结构体cmp,重载“()”运算符.
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
struct cmp{
bool operator()(const int a,const int b) const{
return a%10>b%10;
}
};
int main()
{
int a[]={102,33,45,89,74,22};
priority_queue<int,vector<int>,cmp> q;
for(int i=0;i<6;i++)
{
q.push(a[i]);
}
while(!q.empty())
{
cout<<q.top()<<endl;
q.pop();
}
return 0;
}