C++ STL學習之stack。

bigbigship發表於2014-07-24

stack 介紹

棧是一種容器介面卡,特別為後入先出而設計的一種(LIFO ),那種資料被插入,然後再容器末端取出

棧實現了容器介面卡,這是用了一個封裝了的類作為他的特定容器,提供了一組成員函式去訪問他的元素,元素從特定的容器,也就是堆疊的頭取出袁術。

這個基礎的容器可能是任何標準的容器類,和一些其他特殊設計的模板類,唯一的要求就是要支援一下的操作

  1. <span style="font-size:16px;"><strong>•</strong>back()   
  2. •push_back()   
  3. •pop_back()</span>   

 

因此,標準的容器類别範本vectordeque 和list可以使用,預設情況下,如果沒有容器類被指定成為一個提別的stack 類,標準的容器類别範本就是deque 佇列。


實現C++  STL,棧有兩個引數。

template < class T, class Container = deque<T> > class stack;

引數示意:

  • 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).
  1. // test_stack.cpp : 定義控制檯應用程式的入口點。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <stack>  
  6. #include <vector>  
  7. #include <deque>  
  8. #include <iostream>  
  9.   
  10. using namespace std;  
  11.   
  12. int _tmain(int argc, _TCHAR* argv[])  
  13. {  
  14.     deque<int> mydeque(2,100);  
  15.     vector<int> myvector(2,200);  
  16.   
  17.     stack<int> first;  
  18.     stack<int> second(mydeque);  
  19.   
  20.     stack<int,vector<int> > third;  
  21.     stack<int,vector<int> > fourth(myvector);  
  22.   
  23.     cout << "size of first: " << (int) first.size() << endl;  
  24.     cout << "size of second: " << (int) second.size() << endl;  
  25.     cout << "size of third: " << (int) third.size() << endl;  
  26.     cout << "size of fourth: " << (int) fourth.size() << endl;  
  27.   
  28.   
  29.     return 0;  
  30. }  

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 0false otherwise.

  1. // stack::empty  
  2. #include <iostream>  
  3. #include <stack>  
  4. using namespace std;  
  5.   
  6. int main ()  
  7. {  
  8.   stack<int> mystack;  
  9.   int sum (0);  
  10.   
  11.   for (int i=1;i<=10;i++) mystack.push(i);  
  12.   
  13.   while (!mystack.empty())  
  14.   {  
  15.      sum += mystack.top();  
  16.      mystack.pop();  
  17.   }  
  18.   
  19.   cout << "total: " << sum << endl;  
  20.     
  21.   return 0;  
  22. }  

Output:

total: 55

stack::pop

void pop ( );

在棧的頂部移除元素。

 

  1. // stack::push/pop  
  2. #include <iostream>  
  3. #include <stack>  
  4. using namespace std;  
  5.   
  6. int main ()  
  7. {  
  8.   stack<int> mystack;  
  9.   
  10.   for (int i=0; i<5; ++i) mystack.push(i);  
  11.   
  12.   cout << "Popping out elements...";  
  13.   while (!mystack.empty())  
  14.   {  
  15.      cout << " " << mystack.top();  
  16.      mystack.pop();  
  17.   }  
  18.   cout << endl;  
  19.   
  20.   return 0;  
  21. }  

 

Output:

Popping out elements... 4 3 2 1 0

 

stack::push

void push ( const T& x );

在棧頂新增元素

  1. // stack::push/pop  
  2. #include <iostream>  
  3. #include <stack>  
  4. using namespace std;  
  5.   
  6. int main ()  
  7. {  
  8.   stack<int> mystack;  
  9.   
  10.   for (int i=0; i<5; ++i) mystack.push(i);  
  11.   
  12.   cout << "Popping out elements...";  
  13.   while (!mystack.empty())  
  14.   {  
  15.      cout << " " << mystack.top();  
  16.      mystack.pop();  
  17.   }  
  18.   cout << endl;  
  19.   
  20.   return 0;  
  21. }  

Output:

Popping out elements... 4 3 2 1 0

stack::size

 
size_type size ( ) const;

計算棧物件元素個數

 

  1. // stack::size  
  2. #include <iostream>  
  3. #include <stack>  
  4. using namespace std;  
  5.   
  6. int main ()  
  7. {  
  8.   stack<int> myints;  
  9.   cout << "0. size: " << (int) myints.size() << endl;  
  10.   
  11.   for (int i=0; i<5; i++) myints.push(i);  
  12.   cout << "1. size: " << (int) myints.size() << endl;  
  13.   
  14.   myints.pop();  
  15.   cout << "2. size: " << (int) myints.size() << endl;  
  16.   
  17.   return 0;  
  18. }  



Output:

0. size: 0
1. size: 5
2. size: 4

stack::top

 
      value_type& top ( );
const value_type& top ( ) const;

返回棧頂元素

  1. // test_stack.cpp : 定義控制檯應用程式的入口點。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <stack>  
  6. #include <vector>  
  7. #include <deque>  
  8. #include <iostream>  
  9.   
  10. using namespace std;  
  11.   
  12. int _tmain(int argc, _TCHAR* argv[])  
  13. {  
  14.     stack<int> mystack;  
  15.     mystack.push(10);  
  16.     mystack.push(20);  
  17.     mystack.top()-=5;  
  18.     cout << "mystack.top() is now " << mystack.top() << endl;  
  19.   
  20.     return 0;  
  21. }  

Output:

mystack.top() is now 15

相關文章