C++ STL學習之stack。
stack 介紹
棧是一種容器介面卡,特別為後入先出而設計的一種(LIFO ),那種資料被插入,然後再容器末端取出
棧實現了容器介面卡,這是用了一個封裝了的類作為他的特定容器,提供了一組成員函式去訪問他的元素,元素從特定的容器,也就是堆疊的頭取出袁術。
這個基礎的容器可能是任何標準的容器類,和一些其他特殊設計的模板類,唯一的要求就是要支援一下的操作
- <span style="font-size:16px;"><strong>•</strong>back()
- •push_back()
- •pop_back()</span>
因此,標準的容器類别範本vector, deque 和list可以使用,預設情況下,如果沒有容器類被指定成為一個提別的stack 類,標準的容器類别範本就是deque 佇列。
實現C++ STL,棧有兩個引數。
|
引數示意:
- T: 元素型別
- Container: 被用於儲存和訪問元素的的型別
成員函式
stack::stack
explicit stack ( const Container& ctnr = Container() );
用於構造一個棧介面卡物件。
- ctnr
- Container object
Container is the second class template parameter (the type of the underlying container for thestack; by default: deque<T>, see class description).
- // test_stack.cpp : 定義控制檯應用程式的入口點。
- //
- #include "stdafx.h"
- #include <stack>
- #include <vector>
- #include <deque>
- #include <iostream>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- deque<int> mydeque(2,100);
- vector<int> myvector(2,200);
- stack<int> first;
- stack<int> second(mydeque);
- stack<int,vector<int> > third;
- stack<int,vector<int> > fourth(myvector);
- cout << "size of first: " << (int) first.size() << endl;
- cout << "size of second: " << (int) second.size() << endl;
- cout << "size of third: " << (int) third.size() << endl;
- cout << "size of fourth: " << (int) fourth.size() << endl;
- return 0;
- }
output:
size of first: 0 size of second: 3 size of third: 0 size of fourth: 2 |
stack::empty
bool empty ( ) const;
判斷是否為空。
Return Value
true if the container size is 0, false otherwise.
- // stack::empty
- #include <iostream>
- #include <stack>
- using namespace std;
- int main ()
- {
- stack<int> mystack;
- int sum (0);
- for (int i=1;i<=10;i++) mystack.push(i);
- while (!mystack.empty())
- {
- sum += mystack.top();
- mystack.pop();
- }
- cout << "total: " << sum << endl;
- return 0;
- }
Output:
total: 55 |
stack::pop
void pop ( );
在棧的頂部移除元素。
- // stack::push/pop
- #include <iostream>
- #include <stack>
- using namespace std;
- int main ()
- {
- stack<int> mystack;
- for (int i=0; i<5; ++i) mystack.push(i);
- cout << "Popping out elements...";
- while (!mystack.empty())
- {
- cout << " " << mystack.top();
- mystack.pop();
- }
- cout << endl;
- return 0;
- }
Output:
Popping out elements... 4 3 2 1 0 |
stack::push
void push ( const T& x );
在棧頂新增元素
- // stack::push/pop
- #include <iostream>
- #include <stack>
- using namespace std;
- int main ()
- {
- stack<int> mystack;
- for (int i=0; i<5; ++i) mystack.push(i);
- cout << "Popping out elements...";
- while (!mystack.empty())
- {
- cout << " " << mystack.top();
- mystack.pop();
- }
- cout << endl;
- return 0;
- }
Output:
Popping out elements... 4 3 2 1 0 |
stack::size
size_type size ( ) const;
計算棧物件元素個數
|
Output:
0. size: 0 1. size: 5 2. size: 4 |
stack::top
value_type& top ( ); const value_type& top ( ) const;
返回棧頂元素
- // test_stack.cpp : 定義控制檯應用程式的入口點。
- //
- #include "stdafx.h"
- #include <stack>
- #include <vector>
- #include <deque>
- #include <iostream>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- stack<int> mystack;
- mystack.push(10);
- mystack.push(20);
- mystack.top()-=5;
- cout << "mystack.top() is now " << mystack.top() << endl;
- return 0;
- }
Output:
mystack.top() is now 15 |
相關文章
- C++ STL stackC++
- C++ STL stack容器——棧C++
- C++ 學習筆記之——STL 庫 queueC++筆記
- Stack (stl)
- 跟我學C++中級篇——STL的學習C++
- STL-queue&deque&stack
- C++ STL之迭代器C++
- C++學習筆記 — STL標準模板庫C++筆記
- C++ 學習筆記(1):STL、Vector 與 SetC++筆記
- STL學習
- C++ STL -- vectorC++
- C++ STL -- listC++
- C++ STL -- HashTableC++
- C++ STL listC++
- C++ STL deque容器C++
- C++ stl容器詳解C++
- C++ STL迭代器(iterator)C++
- C++ STL list連結串列C++
- C++【stack/queue】用法和例子C++
- C++初階(stack+queue)C++
- STL容器之deque
- C++提高程式設計-STLC++程式設計
- C++ Templates (2.2 使用Stack類别範本 Use of Class Template Stack )C++
- c++學習C++
- 從零開始學習C++之遞推C++
- 看動畫學演算法之:棧stack動畫演算法
- elastic學習-elastic stack 基礎安全(transport層)AST
- 談談 C++ STL 中的迭代器C++
- 初探STL容器之Vector
- C++ 學習筆記之——輸入和輸出C++筆記
- 資料結構之Stack | 讓我們一塊來學習資料結構資料結構
- C++ Templates (2.1 類别範本Stack的實現 Implementation of Class Template Stack)C++
- 如何學習C++?C++
- C++ Prime 學習C++
- C++ 自我學習C++
- C++學習四C++
- C++學習五C++
- C++學習步驟(C++該如何學)C++
- 【演算法學習】STL庫 大小根堆的用法演算法