c++心得00001-栈

栈是先进后出的数据结构

1.头文件#include <stack>

2.声明 stack<int,char,string等数据类型> 变量名;

3。基本函数。(1)empty看栈是否为空。

                          stack<int> mystack;

                          !mystack.empty() 空返回true;非空返回false

                   (2)push 在栈顶插入数据;

                          stack<int> mystack;

                          mystack.push(10);

                          mystack.push(20);

                    (3)pop 删除栈顶数据                          

                           mystack.pop();

                    (4)top 取栈顶数据

                           mystack.push(10);
                           mystack.push(20);
                           mystack.top() -= 5;
                           cout << "mystack.top() is now " << mystack.top() << endl;

                           输出的是15.

                     (5)size 返回栈的元素个数

                             stack<int> myints;
                             cout << "0. size: " << (int) myints.size() << endl;
                             for (int i=0; i<5; i++) myints.push(i);

                             cout << "1. size: " << (int) myints.size() << endl;

                             输出0和5。

4.清空栈算法。while(!mystack.empty()) mystack.pop();

转载于:https://www.cnblogs.com/stigmj/archive/2012/04/14/2446743.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值