STL - stack(栈)
Stack简介
stack是堆栈容器,是一种“先进后出”的容器。
stack是简单地装饰deque容器而成为另外的一种容器。
#include <stack>
stack对象的默认构造
stack采用模板类实现, stack对象的默认构造形式: stack <T> stkT;
stack <int> stkInt; //一个存放int的stack容器。
stack <float> stkFloat; //一个存放float的stack容器。
stack <string> stkString; //一个存放string的stack容器。
...
//尖括号内还可以设置指针类型或自定义类型。
- stack的push()与pop()方法
- stack.push(elem); //往栈头添加元素
- stack.pop(); //从栈头移除第一个元素
- stack<int> stkInt;
- stkInt.push(1);stkInt.push(3);stkInt.pop();
- stkInt.push(5);stkInt.push(7);
- stkInt.push(9);stkInt.pop();
- stkInt.pop();
- 此时stkInt存放的元素是1,5
- stack对象的拷贝构造与赋值
- stack(const stack &stk); //拷贝构造函数
- stack& operator=(const stack &stk); //重载等号操作符
- stack<int> stkIntA;
- stkIntA.push(1);
- stkIntA.push(3);
- stkIntA.push(5);
- stkIntA.push(7);
- stkIntA.push(9);
- stack<int> stkIntB(stkIntA); //拷贝构造
- stack<int> stkIntC;
- stkIntC = stkIntA; //赋值
- stack的数据存取
- <span style="white-space:pre"> </span>stack.top(); //返回最后一个压入栈元素
- stack<int> stkIntA;
- stkIntA.push(1);
- stkIntA.push(3);
- stkIntA.push(5);
- stkIntA.push(7);
- stkIntA.push(9);
- int iTop = stkIntA.top(); //9
- stkIntA.top() = 19; //19
- stack的大小
- stack.empty(); //判断堆栈是否为空
- stack.size(); //返回堆栈的大小
- stack<int> stkIntA;
- stkIntA.push(1);
- stkIntA.push(3);
- stkIntA.push(5);
- stkIntA.push(7);
- stkIntA.push(9);
- if (!stkIntA.empty())
- {
- int iSize = stkIntA.size(); //5
- }
demo
- #include <iostream>
- #include <cstdio>
- #include <stack>
- #include <algorithm>
- using namespace std;
- void stackInit()
- {
- stack<int> s;
- // 入栈
- for (int i = 0; i < 10; ++i) {
- s.push(i + 1);
- }
- cout << "size of s: " << s.size() << endl;
- while (!s.empty()) {
- cout << s.top() << ' '; // 获取栈顶元素
- s.pop(); // 弹出栈顶元素
- }
- cout << endl;
- }
- class Teacher
- {
- public:
- int age;
- char name[32];
- public:
- void printTeacher()
- {
- cout << "age: " << age << endl;
- }
- };
- void stackClass()
- {
- Teacher t1, t2, t3;
- t1.age = 21;
- t2.age = 22;
- t3.age = 23;
- stack<Teacher> s;
- s.push(t1);
- s.push(t2);
- s.push(t3);
- while (!s.empty()) {
- Teacher tmp = s.top();
- s.pop();
- tmp.printTeacher();
- }
- /*
- age: 23
- age: 22
- age: 21
- */
- cout << endl;
- }
- void stackClassP()
- {
- Teacher t1, t2, t3;
- t1.age = 21;
- t2.age = 22;
- t3.age = 23;
- stack<Teacher *> s;
- s.push(&t1);
- s.push(&t2);
- s.push(&t3);
- while (!s.empty()) {
- Teacher *tmp = s.top();
- s.pop();
- tmp->printTeacher();
- }
- /*
- age: 23
- age: 22
- age: 21
- */
- }
- int main()
- {
- stackInit();
- stackClass();
- stackClassP();
- return 0;
- }
STL - stack(栈)的更多相关文章
- STL --> stack栈
stack栈 c++stack(堆栈)是一个容器的改编,它实现了一个先进后出的数据结构(FILO),使用该容器时需要包含#include<stack>头文件: 定义stack对象示例: s ...
- CodeForces——Game with string(STL stack栈)
Two people are playing a game with a string ss, consisting of lowercase latin letters. On a player's ...
- [STL] stack 栈
在出栈时需要进行两步操作,即先 top( ) 获得栈顶元素,再 pop( ) 删除栈顶元素
- STL之stack栈
栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(First In Last Out, FILO).栈只有一个出口,允许新增元素(只能在栈顶上增加).移出元素(只能移出栈顶 ...
- stack栈
栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(First In Last Out, FILO).栈只有一个出口,允许新增元素(只能在栈顶上增加).移出元素(只能移出栈顶 ...
- C++ STL stack 用法
Stack(栈)是一种后进先出的数据结构,也就是LIFO(last in first out) ,最后加入栈的元素将最先被取出来,在栈的同一端进行数据的插入与取出,这一段叫做“栈顶”. 使用STL的s ...
- STL stack 容器
STL stack 容器 Stack简介 stack是堆栈容器,是一种“先进后出”的容器. stack是简单地装饰deque容器而成为另外的一种容器. #include <s ...
- 浅谈C++ STL stack 容器
浅谈C++ STL stack 容器 本篇随笔简单介绍一下\(C++STL\)中\(stack\)容器的使用方法和常见的使用技巧. stack容器的概念 \(stack\)在英文中是栈的意思.栈是一种 ...
- C++ 标准模板库(STL)-stack
主要介绍一下C++11版本中标准模板库中栈的用法,希望可以帮到需要用的人. #include <iostream> #include <stack> #include < ...
- java - Stack栈和Heap堆的区别
首先分清楚Stack,Heap的中文翻译:Stack—栈,Heap—堆. 在中文里,Stack可以翻译为“堆栈”,所以我直接查找了计算机术语里面堆和栈开头的词语: 堆存储 ...
随机推荐
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/44171115 大家好,欢迎继续回到Android属性动画完全解析.在上一篇文章当中 ...
- 微信小程序基础之表单Form的使用
表单Form的应用很广泛,我们可以利用form设计登录注册,也可以设计一种答题问卷的形式,今天主要讲一下form的使用 form表单,将组件内输入的"switch","i ...
- Gazebo機器人仿真學習探索筆記(五)環境模型
環境模型構建可以通過向其中添加模型實現,待之後補充,比較有趣的是建築物模型, 可以編輯多層樓層和房間,加入樓梯,窗戶和牆壁等,具體可以參考附錄,等有空再補充. 起伏地形環境構建可以參考之前內容:在Ga ...
- static,this,private关键字用法
1:成员变量和局部变量的区别(理解) (1)在类中的位置不同 成员变量:类中方法外 局部变量:方法定义中或者方法声明上 (2)在内存中的位置不同 成员变量:在堆中 局部变量:在栈中 (3)生命周期不同 ...
- Android初级教程对大量数据的做分页处理理论知识
有时候要加载的数据上千条时,页面加载数据就会很慢(数据加载也属于耗时操作).因此就要考虑分页甚至分批显示.先介绍一些分页的理论知识.对于具体用在哪里,会在后续博客中更新. 分页信息 1,一共多少条数据 ...
- Android Device Administration 设备管理器——实现一键锁屏
Android Device Administration 设备管理器--实现一键锁屏 最近研究了一下安全这一块的内容,当然,我是比较水的,所以也拿不出什么好知识点,但是有一些冷门的东西我还是可以聊聊 ...
- 使用git-flow来帮助管理git代码
对git不熟悉的我,经常把git提交搞得很乱,导致在master上有许多无用的commit,最终决定好好地看一下git的使用教程,却不小心发现了还有一个git-flow的工具可以帮助我管理好git项目 ...
- (九十六)借助APNS实现远程通知、后台任务
APNS全称为Apple Push Notification Service,可以实现在app不启动时也能通过服务器推送到iOS端特定设备的功能. APNS的实现原理为先发送设备的UDID和应用的Bu ...
- Android开发学习之路--Android系统架构初探
环境搭建好了,最简单的app也运行过了,那么app到底是怎么运行在手机上的,手机又到底怎么能运行这些应用,一堆的电子元器件最后可以运行这么美妙的界面,在此还是需要好好研究研究.这里从芯片及硬件模块-& ...
- BIP Requests Are Failing With Error "OPP Error Oracle.apps.xdo.XDOException: Error Creating Lock Fil
In this Document Symptoms Cause Solution References Applies to: BI Publisher (formerly XML P ...