棧
一、STL庫中stack的操作
1.stack的定義
首先添加頭文件:
#include <stack>
using namespace std;
定義:
stack<typename> name;
2.stack常用函數(shù)
假設已有棧stack<int> st
top()
獲取棧頂元素(也是stack內(nèi)元素訪問的唯一方法)
int a = st.top();
push()
將元素壓入棧頂(也是stack唯一存入元素的方法)
int x = 985;
st.push(x);
pop()
彈出棧頂元素
st.pop();
empty()
檢測stack是否為空构挤,空則返回true
bool is_empty = st.empty();
size()
返回stack的長度(元素個數(shù))
int size = st.size();
二缸血、題目
leetcode-20.有效的括號
leetcode-155.最小棧
leetcode-232.棧實現(xiàn)隊列
——————————————————————————————————————
隊列
一、STL庫中queue的操作
1.queue的定義
首先添加頭文件:
#include<queue>
using namespace std;
定義:
queue<typename> name;
2.queue常用函數(shù)
假設已有隊列queue<int> q
front()罚渐、back()
queue用front()訪問隊首元素明刷,用back()訪問隊尾元素
int f = q.fornt();
int b = q.back();
push()
push(x)將元素x入隊
q.push(123);
pop()
pop()令隊首元素出隊
q.pop()
empty()
empty()檢查queue是否為kong鸯绿,空則返回true
bool is_empty = q.empty();
size()
返回queue內(nèi)元素個數(shù)
int count = q.size();