1、stack
2、priority_queue #include<queue>
https://blog.youkuaiyun.com/cstopcoder/article/details/18949583
(1)基础介绍
priority_queue 优先队列,其底层是用堆来实现的。
模板声明带有三个参数:
priority_queue<Type, Container, Functional>
其中Type为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。
Container必须是用数组实现的容器,比如 vector, deque 但不能用 list.
STL里面默认用的是 vector.比较方式默认用 operator< , 所以如果你把后面俩个参数缺省的话,
默认的优先队列就是大顶堆,队头元素最大。
//等价 最大优先队列
priority_queue<int> q;
priority_queue<int,vector<int>,less<int> >;//后面有一个空格
其中第二个参数( vector ),是来承载底层数据结构堆的容器,第三个参数( less ),则是一个比较类,less 表示数字大的优先级高,而 greater 表示数字小的优先级高。
//最小优先队列 队头最小
priority_queue<int,vector<int>,greater<int> >q;
优先级队列
队列是先进先出的一种数据结构,优先级队列是按照优先级大小pop出队列
(2)主要操作:
empty():如果队列为空,返回true
pop():删除队列顶部元素
push():队列中加入一个元素
top():查看队列顶部元素
size():返回队列中元素的个数
容器默认是vector,比较方式默认是less,默认比较是operator< ,产生的结果默认是大根堆(最大堆)
(3)几种声明方法:
priority_queue<int> pq1; //大根堆
priority_queue<int, vector<int>, greater<int> > pq2; //知道如何产生小根堆(最小堆)了吧?
priority_queue<Node> pq3 ; //自定义结点,大根堆,此时要重载operator<
priority_queue<Node, vector<Node>, cmp > pq4; //自定义结点,小根堆,cmp是自己写的比较函数,因为greater<Node>没定义,重载opreator>没用