STL stack棧

原文地址:

http://blog.csdn.net/morewindows/article/details/6950881


棧(statck)這種數據結構在計算機中是相當出名的。棧中的數據是先進後出的(First In Last Out, FILO)。棧只有一個出口,允許新增元素(只能在棧頂上增加)、移出元素(只能移出棧頂元素)、取得棧頂元素等操作。在STL中,棧是以別的容器作爲底部結構,再將接口改變,使之符合棧的特性就可以了。因此實現非常的方便。下面就給出棧的函數列表和VS2008中棧的源代碼,在STL中棧一共就5個常用操作函數(top()、push()、pop()、 size()、empty() ),很好記的。

 

VS2008中棧的源代碼

友情提示:初次閱讀時請注意其實現思想,不要在細節上浪費過多的時間。

  1. //VS2008中 stack的定義 MoreWindows整理(http://blog.csdn.net/MoreWindows)  
  2. template<class _Ty, class _Container = deque<_Ty> >  
  3. class stack  
  4. {   // LIFO queue implemented with a container  
  5. public:  
  6.     typedef _Container container_type;  
  7.     typedef typename _Container::value_type value_type;  
  8.     typedef typename _Container::size_type size_type;  
  9.     typedef typename _Container::reference reference;  
  10.     typedef typename _Container::const_reference const_reference;  
  11.   
  12.     stack() : c()  
  13.     {   // construct with empty container  
  14.     }  
  15.   
  16.     explicit stack(const _Container& _Cont) : c(_Cont)  
  17.     {   // construct by copying specified container  
  18.     }  
  19.   
  20.     bool empty() const  
  21.     {   // test if stack is empty  
  22.         return (c.empty());  
  23.     }  
  24.   
  25.     size_type size() const  
  26.     {   // test length of stack  
  27.         return (c.size());  
  28.     }  
  29.   
  30.     reference top()  
  31.     {   // return last element of mutable stack  
  32.         return (c.back());  
  33.     }  
  34.   
  35.     const_reference top() const  
  36.     {   // return last element of nonmutable stack  
  37.         return (c.back());  
  38.     }  
  39.   
  40.     void push(const value_type& _Val)  
  41.     {   // insert element at end  
  42.         c.push_back(_Val);  
  43.     }  
  44.   
  45.     void pop()  
  46.     {   // erase last element  
  47.         c.pop_back();  
  48.     }  
  49.   
  50.     const _Container& _Get_container() const  
  51.     {   // get reference to container  
  52.         return (c);  
  53.     }  
  54.   
  55. protected:  
  56.     _Container c;   // the underlying container  
  57. };  

可以看出,由於棧只是進一步封裝別的數據結構,並提供自己的接口,所以代碼非常簡潔,如果不指定容器,默認是用deque來作爲其底層數據結構的(對deque不是很瞭解?可以參閱《STL系列之一 deque雙向隊列》)。下面給出棧的使用範例:

  1. //棧 stack支持 empty() size() top() push() pop()  
  2. // by MoreWindows(http://blog.csdn.net/MoreWindows)  
  3. #include <stack>  
  4. #include <vector>  
  5. #include <list>  
  6. #include <cstdio>  
  7. using namespace std;  
  8. int main()  
  9. {  
  10.     //可以使用list或vector作爲棧的容器,默認是使用deque的。  
  11.     stack<int, list<int>>      a;  
  12.     stack<int, vector<int>>   b;  
  13.     int i;  
  14.       
  15.     //壓入數據  
  16.     for (i = 0; i < 10; i++)  
  17.     {  
  18.         a.push(i);  
  19.         b.push(i);  
  20.     }  
  21.   
  22.     //棧的大小  
  23.     printf("%d %d\n", a.size(), b.size());  
  24.   
  25.     //取棧項數據並將數據彈出棧  
  26.     while (!a.empty())  
  27.     {  
  28.         printf("%d ", a.top());  
  29.         a.pop();  
  30.     }  
  31.     putchar('\n');  
  32.   
  33.     while (!b.empty())  
  34.     {  
  35.         printf("%d ", b.top());  
  36.         b.pop();  
  37.     }  
  38.     putchar('\n');  
  39.     return 0;  
  40. }  

 

轉載請標明出處,原文地址:http://blog.csdn.net/morewindows/article/details/6950881


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章