首页 > 其他 > 详细

stack栈

时间:2016-12-20 01:31:42      阅读:205      评论:0      收藏:0      [点我收藏+]

 

栈(statck)这种数据结构在计算机中是相当出名的。栈中的数据是先进后出的(First In Last Out, FILO)。栈只有一个出口,允许新增元素(只能在栈顶上增加)、移出元素(只能移出栈顶元素)、取得栈顶元素等操作。在STL中,栈是以别的容器作为底部结构,再将接口改变,使之符合栈的特性就可以了。因此实现非常的方便。下面就给出栈的函数列表和VS2008中栈的源代码,在STL中栈一共就5个常用操作函数(top()、push()、pop()、 size()、empty() ),很好记的。

技术分享

#include<iostream>
#include<stack>
using namespace std;
int main()
{
    stack<int>s;
    int sum(0);
    for(int i=0;i<5;i++)
       s.push(i);
     while(!s.empty())
     {
         cout<<s.top()<<" ";
         sum+=s.top();
        s.pop();
        cout<<s.size()<<endl;
     }
     cout<<sum<<endl;
     cout<<s.size()<<endl;
    return 0;
}

 

stack栈

原文:http://www.cnblogs.com/wft1990/p/6201045.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!