(优先)队列简单总结

就总结下写法吧老是忘啊属实dd

1.队列及相关操作

1 queue <Type> q;
1 q.size();//返回q里元素个数
2 q.empty();//返回q是否为空,空则返回1,否则返回0
3 q.push(k);//末尾插入k
4 q.pop();//删掉q的第一个元素
5 q.top();//返回q的第一个元素 ≈ q.front();
6 q.back();//返回q的末尾元素

2.简单优先队列:默认降序队列

1 priority_queue <Type> q;

3.自定义优先队列

先把原型摆上: priority_queue<Type, Container, Functional>

 简单(其实可以归到2里,不过为了对应这个原型还是放到这吧):

1 //升序队列 
2 priority_queue <Type,vector<Type>,greater<Type> > q; 
3 
4 //降序队列
5  priority_queue <Type,vector<Type>,less<Type> >q; 

 结构体:

 1 //(一) 另写比较实现降序队列
 2 struct tmp1 { int x; int y; };
 3 
 4 struct tmp2 {
 5     bool operator() (tmp1 a, tmp1 b) {
 6         return a.y < b.y; 
 7     }
 8 };
 9 
10 priority_queue<tmp1, vector<tmp1>, tmp2> f;
 1 //(二)直接重载运算符实现降序队列
 2 struct tmp1 {
 3      int x; int y;
 4      tmp1(int a,int b) {x = a; y=b;}
 5      bool operator<(const tmp1& c) const{
 6          return y < c.y; 
 7     }
 8 };
 9 
10 priority_queue<tmp1> d;
扫描二维码关注公众号,回复: 5083234 查看本文章

猜你喜欢

转载自www.cnblogs.com/noobimp/p/10325350.html
今日推荐