C++(STL):24 ---序列式容器stack用法
生活随笔
收集整理的這篇文章主要介紹了
C++(STL):24 ---序列式容器stack用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.stack的定義
要使用stack,應先添加頭文件#include <stack>, 并在頭文件下面加上 "using namespace std" //定義 stack< typename > name;2. stack容器內元素的訪問
由于棧(stack)本書就是一種后進先出的數據結構,在STL的stack中只能 通過top()來訪問棧頂元素. #include <stdio.h> #include <stack> using namespace std; int main() {stack<int> st;for(int i = 1; i <= 5; i++) {st.push(i); //push(i)用以把i壓入棧,故此處依次入棧 1 2 3 4 5}printf("%d\n", st.top()); //top取棧頂元素return 0; }3. stack常用函數實例解析
(1) push()
//push(x)將x入棧,時間復雜度為O(1)(2) top()
//top()獲得棧頂元素,時間總結
以上是生活随笔為你收集整理的C++(STL):24 ---序列式容器stack用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 记一次海外大型SLG游戏服务器进程被OO
- 下一篇: C++(STL):07---vector