……

(优先)队列简单总结

就总结下写法吧老是忘啊属实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; 

结构体:

 //(一) 另写比较实现按y降序队列
 struct tmp1 { int x; int y; };
 
 struct tmp2 {
     bool operator() (tmp1 a, tmp1 b) {
         return a.y < b.y; 
     }
 };
 
priority_queue<tmp1, vector<tmp1>, tmp2> f;

 

//(二)直接重载运算符实现按y降序队列
struct tmp1 {
      int x; int y;
      tmp1(int a,int b) {x = a; y=b;}
      bool operator<(const tmp1& c) const{
          return y < c.y; 
     }
};

priority_queue<tmp1> d;

 

posted @ 2019-01-26 23:58  noobimp  阅读(201)  评论(0编辑  收藏  举报