1. 程式人生 > >C++_STL_資料結構_stack_棧

C++_STL_資料結構_stack_棧

棧(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.     typedeftypename _Container::value_type value_type;  
  8.     typedeftypename _Container::size_type size_type;  
  9.     typedef
    typename _Container::reference reference;  
  10.     typedeftypename _Container::const_reference const_reference;  
  11.     stack() : c()  
  12.     {   // construct with empty container
  13.     }  
  14.     explicit stack(const _Container& _Cont) : c(_Cont)  
  15.     {   // construct by copying specified container
  16.     }  
  17.     bool
     empty() const
  18.     {   // test if stack is empty
  19.         return (c.empty());  
  20.     }  
  21.     size_type size() const
  22.     {   // test length of stack
  23.         return (c.size());  
  24.     }  
  25.     reference top()  
  26.     {   // return last element of mutable stack
  27.         return (c.back());  
  28.     }  
  29.     const_reference top() const
  30.     {   // return last element of nonmutable stack
  31.         return (c.back());  
  32.     }  
  33.     void push(const value_type& _Val)  
  34.     {   // insert element at end
  35.         c.push_back(_Val);  
  36.     }  
  37.     void pop()  
  38.     {   // erase last element
  39.         c.pop_back();  
  40.     }  
  41.     const _Container& _Get_container() const
  42.     {   // get reference to container
  43.         return (c);  
  44.     }  
  45. protected:  
  46.     _Container c;   // the underlying container
  47. };  

可以看出,由於棧只是進一步封裝別的資料結構,並提供自己的介面,所以程式碼非常簡潔,如果不指定容器,預設是用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. usingnamespace 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.     for (i = 0; i < 10; i++)  
  16.     {  
  17.         a.push(i);  
  18.         b.push(i);  
  19.     }  
  20.     //棧的大小
  21.     printf("%d %d\n", a.size(), b.size());  
  22.     //取棧項資料並將資料彈出棧
  23.     while (!a.empty())  
  24.     {  
  25.         printf("%d ", a.top());  
  26.         a.pop();  
  27.     }  
  28.     putchar('\n');  
  29.     while (!b.empty())  
  30.     {  
  31.         printf("%d ", b.top());  
  32.         b.pop();  
  33.     }  
  34.     putchar('\n');  
  35.     return 0;  
  36. }