參考資料:
STL 在 OI 中的應用
stack
stack 后入先出(LIFO)棧
頭文件:
#include<stack>
定義:
stack<int> s;
函數:
函數 | 功能 |
---|---|
q.top() | 獲取棧頂元素(並不刪除) |
q.pop() | 刪除棧頂元素 |
q.push(x) | 向棧中加入元素 |
q.empty() | 判斷棧是否為空 |
queue
queue 先入先出(FIFO)隊列
頭文件:
#include<queue>
定義:
queue<int> q;
函數:
函數 | 功能 |
---|---|
q.front() | 獲取隊首元素(並不刪除) |
q.pop() | 刪除隊首元素 |
q.push(x) | 向隊列中加入元素 |
q.empty() | 判斷隊列是否為空 |
priority_queue 優先隊列
頭文件:
#include<queue>
定義:
priority_queue<int> q; // 隊頭最大
priority_queue<int,vector<int>,greater<int> > q; 隊頭最小
函數:
函數 | 功能 |
---|---|
q.top() | 獲取優先隊列中最大的元素(並不刪除),其時間復雜度為\(O(1)\) |
q.pop() | 刪除優先隊列中最大元素,其時間復雜度為\(O(log n)\) |
q.push(x) | 向優先隊列中加入元素,其時間復雜度為\(O(log n)\) |
q.empty() | 判斷優先隊列是否為空 |