stack(栈)

本文介绍了C++ STL中的stack容器,强调了其先进后出的特点,并提供了VS2008中stack的源代码实现。stack默认使用deque作为底层数据结构,但也支持list或vector。示例代码展示了如何使用stack进行数据压入、查询栈的大小、获取栈顶元素及弹出元素等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

stack 栈容器特点  先进后出,类似于一个瓶子,最先进去的要最后才能出来;


常用函数

empty() 堆栈为空则返回真 
pop() 移除栈顶元素
push() 在栈顶增加元素
size() 返回栈中元素数目
top() 返回栈顶元素

queue:
back() 返回最后一个元素
empty() 如果队列空则返回真
front() 返回第一个元素
pop() 删除第一个元素
push() 在末尾加入一个元素
size() 返回队列中元素的个数


VS2008中栈的源代码

友情提示:初次阅读时请注意其实现思想,不要在细节上浪费过多的时间。

[cpp]  view plain  copy
  1. //VS2008中 stack的定义 MoreWindows整理(http://blog.youkuaiyun.com/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双向队列》)。下面给出栈的使用范例:

[cpp]  view plain  copy
  1. //栈 stack支持 empty() size() top() push() pop()  
  2. // by MoreWindows(http://blog.youkuaiyun.com/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.youkuaiyun.com/morewindows/article/details/6950881


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值