看了一上午书换了一上午皮肤,终于会了。
模板原形:
template <class T, class Container = vector<T>,
class Compare = less< typename Container::value_type> > class priority_queue;
T是数据类型,Container 是保留数据的容器,Functional是元素的比较方式。
Container必须是用数组实现的容器,比如vector,deque但是不能是list。
STL 里面容器默认的用的是vector,比较方式默认用 < (即越小优先级越低)
下面代码是我自己写的,自定义比较方式的用法之一:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Node{
int x;
int y;
friend ostream & operator << (ostream & os,const Node & tmep);
friend istream & operator >> (istream & is,Node & temp);
};
struct cmp{
bool operator () (const Node & a,const Node & b)const{ //据说这个叫仿函数,我也不知道是个什么东西,先这么用着吧;
return (a.x + a.y)>(b.x + b.y);
}
};
istream & operator >> (istream & is,Node & temp){
is>>temp.x>>temp.y;
return is;
}
ostream & operator << (ostream & os,const Node & temp){
os<<temp.x<<' '<<temp.y;
return os;
}
priority_queue<Node,vector<Node>,cmp> q;
int main(){
// freopen("Data.txt","r",stdin);
Node temp;
while(cin>>temp)
q.push(temp);
while(!q.empty()){
cout<<q.top()<<endl;
q.pop();
}
return 0;
}