1、stack簡介
????????stack是實現的一個先進后出,后進先出的容器。它只有一個出口,只能操作最頂端元素。
2、stack庫函數
(1)push()? ? ? ?//向棧壓入一個元素
(2)pop()? ? ? ? ?//移除棧頂元素
(3)top()? ? ? ?? ?//返回棧頂元素值
(4)empty()? ? ?//返回bool型,表示棧內是否為空,【true為空,false為非空 】
(5)size()? ? ? ? //返回棧內元素個數
3、示例
#include <stack>
#include <iostream>
using namespace std;int main()
{stack<int> st;st.push(5418);cout << st.top() << endl;cout << st.size() << endl;st.push(54);cout << st.top() << endl;st.pop();cout << st.top() << endl;return 0;
}
結果為: