当前位置: 首页 > news >正文

笔记(六)——stack容器的基础理论知识

stack是堆栈容器,元素遵循先进后出的顺序。

头文件:#include<stack>

一、stack容器的对象构造方法

stack采用模板类实现默认构造

例如stack<T> vecT;

#include<iostream>
#include<stack>
using namespace std;
int main()
{stack<int> stInt;stack<float> stFloat;stack<string> stString;stInt.push(5);//在栈头添加元素stInt.pop();//在栈头删除元素stInt.push(6);//在栈头添加元素stInt.push(7);//在栈头添加元素stInt.push(8);//在栈头添加元素stInt.pop();//在栈头删除元素while(!stInt.empty())    {cout<<stInt.top()<<endl;stInt.pop();//在栈头删除元素} //输出7,6   return 0;
}

stack对象的带参构造方式

1、stack<T> st1(st2);拷贝构造函数

2、stack& operator=(const stack &st);重载等号操作符。

#include<iostream>
#include<stack>
using namespace std;
int main()
{stack<int> stInt1;stInt1.push(5);//在栈头添加元素stInt1.pop();//在栈头删除元素stInt1.push(6);//在栈头添加元素stInt1.push(7);//在栈头添加元素stInt1.push(8);//在栈头添加元素stInt1.pop();//在栈头删除元素stack<int> stInt2(stInt1);stack<int> stInt3=stInt1;while(!stInt1.empty())    {cout<<stInt1.top()<<endl;stInt1.pop();//在栈头删除元素} //输出7,6   while(!stInt2.empty())    {cout<<stInt2.top()<<endl;stInt2.pop();//在栈头删除元素} //输出7,6 while(!stInt3.empty())    {cout<<stInt3.top()<<endl;stInt3.pop();//在栈头删除元素} //输出7,6 return 0;
}

二、stack容器的大小

  1. stack.empty();//判断堆栈是否为空

  1. stack.size();//返回堆栈的大小

#include<iostream>
#include<stack>
using namespace std;
int main()
{stack<int> stInt1;stInt1.push(5);//在栈头添加元素stInt1.pop();//在栈头删除元素stInt1.push(6);//在栈头添加元素stInt1.push(7);//在栈头添加元素stInt1.push(8);//在栈头添加元素stInt1.pop();//在栈头删除元素int size=stInt1.size();cout<<size<<endl;//输出2    while(!stInt1.empty())    {cout<<stInt1.top()<<endl;stInt1.pop();//在栈头删除元素} //输出7,6   return 0;
}
http://www.lryc.cn/news/24828.html

相关文章:

  • Web前端学习:四 - 练习
  • odoo15 标题栏自定义
  • 视觉SLAM十四讲 ch3 (三维空间刚体运动)笔记
  • 问题解决:java.net.SocketTimeoutException: Read timed out
  • 前端代码优化方法
  • 【批处理脚本】-1.16-文件内字符串查找增强命令findstr
  • 三天吃透Redis面试八股文
  • 数据湖架构Hudi(三)Hudi核心概念
  • 在数字优先的世界中打击知识产权盗窃
  • 机器学习算法原理——逻辑斯谛回归
  • 【华为OD机试 】最优资源分配/芯片资源占用(C++ Java JavaScript Python)
  • 600 条最强 Linux 命令总结
  • python自学之《21天学通Python》(15)——第18章 数据结构基础
  • 从功能到自动化,熬夜3天整理出这一份2000字学习指南~
  • 客户端攻击(溯源攻击,获取客户端信息)
  • visual studio 2022 社区版 c# 环境搭建及安装使用【图文解析-小白版】
  • 21- 神经网络模型_超参数搜索 (TensorFlow系列) (深度学习)
  • 《NFL橄榄球》:芝加哥熊·橄榄1号位
  • 【ES】Elasticsearch核心基础概念:文档与索引
  • 实时手势识别(C++与python都可实现)
  • 15个Spring扩展点,一般人知道的不超过5个!
  • Elasticsearch:以 “Painless” 方式保护你的映射
  • js几种对象创建方式
  • 阿里云服务器ECS适用于哪些应用场景?
  • Ajax学习笔记01
  • Jinja2----------过滤器的使用、控制语句
  • 面试了1个自动化测试,开口40W年薪,只能说痴人做梦...
  • 冲鸭!33% 程序员月薪达到 5 万元以上~
  • 【RSA】HTTPS中SSL/TLS握手时RSA前后端加密流程
  • clion在linux设置桌面启动图标(jetbrains全家桶均适用)