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容器。

...

//尖括号内还可以设置指针类型或自定义类型。

  1. stackpush()与pop()方法
  2. stack.push(elem); //往栈头添加元素
  3. stack.pop(); //从栈头移除第一个元素
  4.  
  5. stack<int> stkInt;
  6. stkInt.push(1);stkInt.push(3);stkInt.pop();
  7. stkInt.push(5);stkInt.push(7);
  8. stkInt.push(9);stkInt.pop();
  9. stkInt.pop();
  10. 此时stkInt存放的元素是1,5
  11. stack对象的拷贝构造与赋值
  12. stack(const stack &stk); //拷贝构造函数
  13. stack& operator=(const stack &stk); //重载等号操作符
  14.  
  15. stack<int> stkIntA;
  16. stkIntA.push(1);
  17. stkIntA.push(3);
  18. stkIntA.push(5);
  19. stkIntA.push(7);
  20. stkIntA.push(9);
  21.  
  22. stack<int> stkIntB(stkIntA); //拷贝构造
  23. stack<int> stkIntC;
  24. stkIntC = stkIntA; //赋值
  25. stack的数据存取
  26. <span style="white-space:pre"> </span>stack.top(); //返回最后一个压入栈元素
  27. stack<int> stkIntA;
  28. stkIntA.push(1);
  29. stkIntA.push(3);
  30. stkIntA.push(5);
  31. stkIntA.push(7);
  32. stkIntA.push(9);
  33.  
  34. int iTop = stkIntA.top(); //9
  35. stkIntA.top() = 19; //19
  36. stack的大小
  37. stack.empty(); //判断堆栈是否为空
  38. stack.size(); //返回堆栈的大小
  39.  
  40. stack<int> stkIntA;
  41. stkIntA.push(1);
  42. stkIntA.push(3);
  43. stkIntA.push(5);
  44. stkIntA.push(7);
  45. stkIntA.push(9);
  46.  
  47. if (!stkIntA.empty())
  48. {
  49. int iSize = stkIntA.size(); //5
  50. }

demo

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <stack>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. void stackInit()
  8. {
  9. stack<int> s;
  10.  
  11. // 入栈
  12. for (int i = 0; i < 10; ++i) {
  13. s.push(i + 1);
  14. }
  15. cout << "size of s: " << s.size() << endl;
  16.  
  17. while (!s.empty()) {
  18. cout << s.top() << ' '; // 获取栈顶元素
  19. s.pop(); // 弹出栈顶元素
  20. }
  21. cout << endl;
  22. }
  23.  
  24. class Teacher
  25. {
  26. public:
  27. int age;
  28. char name[32];
  29. public:
  30. void printTeacher()
  31. {
  32. cout << "age: " << age << endl;
  33. }
  34. };
  35.  
  36. void stackClass()
  37. {
  38. Teacher t1, t2, t3;
  39. t1.age = 21;
  40. t2.age = 22;
  41. t3.age = 23;
  42.  
  43. stack<Teacher> s;
  44. s.push(t1);
  45. s.push(t2);
  46. s.push(t3);
  47.  
  48. while (!s.empty()) {
  49. Teacher tmp = s.top();
  50. s.pop();
  51. tmp.printTeacher();
  52. }
  53. /*
  54. age: 23
  55. age: 22
  56. age: 21
  57. */
  58. cout << endl;
  59. }
  60.  
  61. void stackClassP()
  62. {
  63. Teacher t1, t2, t3;
  64. t1.age = 21;
  65. t2.age = 22;
  66. t3.age = 23;
  67.  
  68. stack<Teacher *> s;
  69. s.push(&t1);
  70. s.push(&t2);
  71. s.push(&t3);
  72.  
  73. while (!s.empty()) {
  74. Teacher *tmp = s.top();
  75. s.pop();
  76. tmp->printTeacher();
  77. }
  78. /*
  79. age: 23
  80. age: 22
  81. age: 21
  82. */
  83. }
  84.  
  85. int main()
  86. {
  87. stackInit();
  88. stackClass();
  89. stackClassP();
  90.  
  91. return 0;
  92. }

STL - stack(栈)的更多相关文章

  1. STL --> stack栈

    stack栈 c++stack(堆栈)是一个容器的改编,它实现了一个先进后出的数据结构(FILO),使用该容器时需要包含#include<stack>头文件: 定义stack对象示例: s ...

  2. 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 ...

  3. [STL] stack 栈

    在出栈时需要进行两步操作,即先 top( ) 获得栈顶元素,再 pop( ) 删除栈顶元素

  4. STL之stack栈

    栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(First In Last Out, FILO).栈只有一个出口,允许新增元素(只能在栈顶上增加).移出元素(只能移出栈顶 ...

  5. stack栈

    栈(statck)这种数据结构在计算机中是相当出名的.栈中的数据是先进后出的(First In Last Out, FILO).栈只有一个出口,允许新增元素(只能在栈顶上增加).移出元素(只能移出栈顶 ...

  6. C++ STL stack 用法

    Stack(栈)是一种后进先出的数据结构,也就是LIFO(last in first out) ,最后加入栈的元素将最先被取出来,在栈的同一端进行数据的插入与取出,这一段叫做“栈顶”. 使用STL的s ...

  7. STL stack 容器

    STL stack 容器 Stack简介 stack是堆栈容器,是一种“先进后出”的容器.      stack是简单地装饰deque容器而成为另外的一种容器.      #include <s ...

  8. 浅谈C++ STL stack 容器

    浅谈C++ STL stack 容器 本篇随笔简单介绍一下\(C++STL\)中\(stack\)容器的使用方法和常见的使用技巧. stack容器的概念 \(stack\)在英文中是栈的意思.栈是一种 ...

  9. C++ 标准模板库(STL)-stack

    主要介绍一下C++11版本中标准模板库中栈的用法,希望可以帮到需要用的人. #include <iostream> #include <stack> #include < ...

  10. java - Stack栈和Heap堆的区别

    首先分清楚Stack,Heap的中文翻译:Stack—栈,Heap—堆.         在中文里,Stack可以翻译为“堆栈”,所以我直接查找了计算机术语里面堆和栈开头的词语:        堆存储 ...

随机推荐

  1. Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/44171115 大家好,欢迎继续回到Android属性动画完全解析.在上一篇文章当中 ...

  2. 微信小程序基础之表单Form的使用

    表单Form的应用很广泛,我们可以利用form设计登录注册,也可以设计一种答题问卷的形式,今天主要讲一下form的使用 form表单,将组件内输入的"switch","i ...

  3. Gazebo機器人仿真學習探索筆記(五)環境模型

    環境模型構建可以通過向其中添加模型實現,待之後補充,比較有趣的是建築物模型, 可以編輯多層樓層和房間,加入樓梯,窗戶和牆壁等,具體可以參考附錄,等有空再補充. 起伏地形環境構建可以參考之前內容:在Ga ...

  4. static,this,private关键字用法

    1:成员变量和局部变量的区别(理解) (1)在类中的位置不同 成员变量:类中方法外 局部变量:方法定义中或者方法声明上 (2)在内存中的位置不同 成员变量:在堆中 局部变量:在栈中 (3)生命周期不同 ...

  5. Android初级教程对大量数据的做分页处理理论知识

    有时候要加载的数据上千条时,页面加载数据就会很慢(数据加载也属于耗时操作).因此就要考虑分页甚至分批显示.先介绍一些分页的理论知识.对于具体用在哪里,会在后续博客中更新. 分页信息 1,一共多少条数据 ...

  6. Android Device Administration 设备管理器——实现一键锁屏

    Android Device Administration 设备管理器--实现一键锁屏 最近研究了一下安全这一块的内容,当然,我是比较水的,所以也拿不出什么好知识点,但是有一些冷门的东西我还是可以聊聊 ...

  7. 使用git-flow来帮助管理git代码

    对git不熟悉的我,经常把git提交搞得很乱,导致在master上有许多无用的commit,最终决定好好地看一下git的使用教程,却不小心发现了还有一个git-flow的工具可以帮助我管理好git项目 ...

  8. (九十六)借助APNS实现远程通知、后台任务

    APNS全称为Apple Push Notification Service,可以实现在app不启动时也能通过服务器推送到iOS端特定设备的功能. APNS的实现原理为先发送设备的UDID和应用的Bu ...

  9. Android开发学习之路--Android系统架构初探

    环境搭建好了,最简单的app也运行过了,那么app到底是怎么运行在手机上的,手机又到底怎么能运行这些应用,一堆的电子元器件最后可以运行这么美妙的界面,在此还是需要好好研究研究.这里从芯片及硬件模块-& ...

  10. 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 ...