目录:
- stack
- queue
- priority_queue
stack
头文件
#include<stack>
using namespace std;
创建
stack<type_name> s;
常用成员函数
1 | s.empty(); //bool 判空 |
queue
头文件
#include<queue>
using namespace std;
创建
queue<type_name> q
常用成员方法
1 | q.push(elem); //void 将元素插入队列尾 |
priority_queue
头文件
#include<queue>
using namespace std;
创建
priority_queue<type_name> p //大顶堆,优先级从高到低自动有序
改变排序结果
priority_queue<type,vector<type>,greater<type> >p //不写greater默认less,即从大到小排序
自定义优先级,重载<符号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using namespace std;
struct Node{
int x,y;
Node(int a=0, int b=0):
x(a), y(b) {}
};
struct cmp{
bool operator()(Node a, Node b){
//优先按照x从小到大排,若x相等,按y从小到大。勿反!!!
if(a.x == b.x) return a.y>b.y;
return a.x>b.x;
}
};
int main(){
priority_queue<Node, vector<Node>, cmp>p;
for(int i=0; i<10; ++i)
p.push(Node(rand(), rand()));
while(!p.empty()){
cout<<p.top().x<<' '<<p.top().y<<endl;
p.pop();
}
return 0;
}
常用成员方法
1 | p.push(elem); //void 将元素插入队列尾 |