当前位置:网站首页>STL--stack容器
STL--stack容器
2022-07-17 00:14:00 【hjl011006】
一、stack的基本知识
- 概念:stack 是一种先进后出的数据结构,他只有一个出口
- 演示:

- 特点:
栈中只有栈顶元素才可以被外界访问,因此不允许有性遍历行为
栈中进入数据称为-----入栈(push)
栈中弹出数据称为-----出栈(pop)
二、stack常用接口
1.构造函数
- stack<T> stk; // stack采用模板类实现,stack对象的默认构造形式
- stack(const stack & stk); // 拷贝构造函数
2.赋值操作
- stack & operator=(const stack & stk); // 重载等号操作符
3.数据存取
- push(elem); // 向栈顶添加元素
- pop(); // 从栈顶移除一个元素
- top(); // 返回栈顶元素
4.大小操作
- empty(); // 判断堆栈是否为空
- size(); // 返回栈的大小
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
stack<int> s;
for(int i=0;i<10;i++)
{
s.push(i);
}
while(!s.empty())
{
cout<<"栈的大小;"<<s.size()<<"\t";
// 查看栈顶元素
cout<<"栈顶元素为:"<<s.top()<<endl;
// 出栈
s.pop();
}
cout<<"栈的大小:"<<s.size()<<endl;
return 0;
}
边栏推荐
猜你喜欢

C语言程序之编译,链接

Basic principle and parameter interpretation of operational amplifier

gdb+vscode进行调试3——vscode以及gdb远程调试

笔记一之IDL基础内容:常用数据类型_创建数组_类型转换_print输出_基本运算_关系运算

On the properties and methods of list < t >

Dueling DQN的理论基础及其代码实现【Pytorch + Pendulum-v0】

03 design of urban road dedusting and cooling system based on ZigBee

Vmware Tools最新安装教程(RHEL8)

Static library and dynamic library

Uncaught syntaxerror: unexpected token '< is reported on the blank page of the H5 uniapp package‘
随机推荐
Analysis of IGBT direct short circuit process
Leetcode 322: Coin Change - 动态规划
运算放大器基本原理与参数解读
【解决方案】win11中本地组策略编辑器(gpedit.msc)打不开
Monitor browser return operation - prohibit returning to the previous page
笔记一之IDL基础内容:常用数据类型_创建数组_类型转换_print输出_基本运算_关系运算
ENVI_ Idl: batch Reproject MODIS swath products and specify the range output as GeoTIFF format + parsing
CMake常用命令
池式组件之内存池篇
Oozie 集成 Sqoop
01基于RFID的智能仓储管理系统设计
Double Q-Learning理论基础及其代码实现【Pendulum-v0】
ENVI_ Idl: average calculation + analysis of MODIS swath products in batches
浏览器无法打开Tensorboard
保留两位小数,并向上取值
捉虱子的博弈论
ENVI_IDL:读取所有OMI产品的NO2柱含量并计算月均值、季均值、年均值+解析
DQN理论基础及其代码实现【Pytorch + CartPole-v0】
Labelme 的简单用法和界面介绍
SAE j1708/j1587 protocol details