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可以翻译为“堆栈”,所以我直接查找了计算机术语里面堆和栈开头的词语: 堆存储 ...
随机推荐
- 20160222.CCPP体系详解(0032天)
程序片段(01):宽字符.c+字符串与内存四区.c 内容概要:宽窄字符 ///宽字符.c #include <stdio.h> #include <stdlib.h> #inc ...
- springMVC源码分析--HttpRequestHandlerAdapter(四)
上一篇博客springMVC源码分析--HandlerAdapter(一)中我们主要介绍了一下HandlerAdapter接口相关的内容,实现类及其在DispatcherServlet中执行的顺序,接 ...
- WmS详解(一)之token到底是什么?基于Android7.0源码
做Android有些年头了,Framework层三大核心View系统,WmS.AmS最近在研究中,这三大块,每一块都够写一个小册子来介绍,其中View系统的介绍,我之前有一个系列的博客(不过由于时间原 ...
- bitbucket添加ssh key
右上角头像->bitbucket settings->ssh keys 直接点击: https://bitbucket.org/account/user/.../ssh-keys/ 教程: ...
- UE4 读取本地图片
参考链接:https://answers.unrealengine.com/questions/235086/texture-2d-shows-wrong-colors-from-jpeg-on-ht ...
- JAVA面向对象-----接口的概述
接口的概述 **接口(interface):**usb接口,主要是使用来拓展笔记本的功能,那么在java中的接口主要是使用来拓展定义类的功能,可以弥补java中单继承的缺点. class Pencil ...
- (一三〇)UITextField的光标操作扩展
简介 在iOS开发中,有时候需要完全自主的定义键盘,用于完整的单词输入,例如计算机应用中,需要一次性的输入sin(,在移动光标时要完整的跳过sin(,在删除时也要完整的删除,这就需要对光标的位置进行精 ...
- GC垃圾回收算法
什么是GC垃圾回收呢.日常生活中我们去餐厅吃饭吃完饭,吃完饭走了餐具不用管,服务员在把餐具拿走,这是一种方式,服务员怎么知道他要来把餐具拿走呢,因为你走了,这个位置空了.服务员什么时候拿走餐具很重要, ...
- UNIX网络编程——客户/服务器程序设计示范(三)
TCP预先派生子进程服务器程序,accept无上锁保护 我们的第一个"增强"型服务器程序使用称为预先派生子进程的技术.使用该技术的服务器不像传统意义的并发服务器那样为每个客户现场派 ...
- Android简易实战教程--第五话《开发一键锁屏应用》
转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/51860900 点击打开链接 Device Administration 对于这个应 ...