c++优先队列中重载用算符
上代码:
#include<queue>
#include<iostream>
using namespace std;
struct node //对结构体进行队列运算符重载。
{
int x,y;
bool operator < (const node& a)const{
return x>a.x;//从小到大
}
}e[120];
struct cmp//对普通数组一类进行重排序。
{
bool operator ()(int &a,int &b)
{
return a>b;
}
};
int main()
{
int i;
vector<int> v;
priority_queue<int,vector<int>,cmp> q;
priority_queue<node> p;
v.push_back(5);
v.push_back(4);
v.push_back(3);
vector<int>::iterator it;
for(it=v.begin();it!=v.end();it++)
{
q.push(*it);
}
while(!q.empty())
{
cout<<q.top()<<" ";
q.pop();
}
cout<<endl;
for(i=0;i<20;i++)
{
e[i].x=-1*i;
e[i].y=i;
p.push(e[i]);
}
while(!p.empty())
{
cout<<p.top().x<<" "<<p.top().y<<endl;
p.pop();
}
system("pause");
}