STL 栈和队列

基本用法:

 1 ///栈:
 2 s.empty() ///判断栈是否为空
 3 s.size() ///返回栈中元素个数
 4 s.pop() ///删除栈顶元素
 5 s.top()//返回栈顶元素
 6 s.push(x) ///在栈顶压入新元素
 7 
 8 ///队列:
 9 q.empty() ///判断队列是否为空 
10 q.size() /// 返回队列中元素个数  
11 q.pop()  ///删除队列首元素  
12 q.front()  /// 返回队首元素
13 q.push(x)  ///在队尾压入新元素
14 q.back()  ///返回队列尾元素的值

优先队列:

1 priority_queue <node> que;
2 ///默认队头小

优先队列拓展:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <queue>
 6 #include <stack>
 7 #include <vector>
 8 
 9 using namespace std;
10 
11 struct node{
12     int data;
13     bool operator < (const node &othe) const{
14         return data > othe.data; ///从小到大,反
15     }
16 };
17 
18 priority_queue <node> que;
19 
20 int main()
21 {
22     node n;
23     n.data = 5;
24     que.push(n);
25     n.data = 7;
26     que.push(n);
27     n.data = 3;
28     que.push(n);
29 
30     while( !que.empty()){
31         printf("%d
",que.top().data);
32         que.pop();
33     }
34 
35     return 0;
36 }
原文地址:https://www.cnblogs.com/0xiaoyu/p/12855843.html