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

C++ STL stack容器使用详解

一、stack容器概述

stack容器适配器是C++标准模板库(STL)中实现后进先出(LIFO)数据结构的重要组件,它通过封装底层容器(如deque/vector/list)提供栈操作接口。

二、stack核心操作详解

1. 容器构造方式

// 默认使用deque存储元素
stack<int> staInt;  // 显式指定底层容器
stack<int, vector<int>> vecStack;
stack<int, list<int>> listStack;
stack<int, deque<int>> dequeStack;
  • deque:默认底层容器,支持快速首尾操作
  • vector:需要包含头文件<vector>
  • list:需要包含头文件<list>

2. 元素操作函数

staInt.push(1);  // 压栈操作
staInt.push(2);
staInt.pop();    // 弹出栈顶元素(需保证栈非空)
staInt.push(3);

3. 栈顶访问与修改

int& iTop = staInt.top();  // 获取栈顶的引用
iTop = 66;                // 通过引用直接修改栈顶值
staInt.top() = 88;        // 直接修改栈顶元素

4. 容器状态查询

cout << "栈大小: " << staInt.size() << endl;
cout << "是否为空: " << boolalpha << staInt.empty() << endl;

5. 栈遍历与清空

while (!staInt.empty()) {cout << staInt.top() << " ";staInt.pop();  // 必须弹出才能访问下层元素
}

三、关键特性解析

  1. 底层容器选择

    • vector:动态数组实现,push/pop需要O(1)摊销时间
    • list:链表实现,所有操作保证O(1)时间复杂度
    • deque(默认):分块数组实现,综合性能最优
  2. 元素访问特性

    • top()返回引用,可直接修改栈顶元素
    • 修改栈顶元素不会改变栈的结构特性
  3. 安全操作规范

    • 调用pop()前必须确保栈非空
    • 使用empty()判断替代size() == 0更高效

四、典型应用场景

  1. 函数调用栈模拟
  2. 括号匹配验证
  3. 表达式求值(逆波兰式)
  4. 撤销操作实现

五、完整代码回顾

#include <iostream>
#include <vector>
#include <stack>
#include <list>
#include <deque>using namespace std;int main(void) {// stack对象的默认构造// stack<int> staInt;    // 默认使用deque存储元素// stack<int, vector<int>> staInt;// stack<int, list<int>> staInt;stack<int, deque<int>> staInt;// stack的push()与pop()方法staInt.push(1);staInt.push(2);// staInt.pop();staInt.push(3);// int iTop = staInt.top();int& iTop = staInt.top();iTop = 66;cout << "staInt.top: " << staInt.top() << endl;staInt.top() = 88;cout << "staInt.size: " << staInt.size() << endl;cout << "staInt.top: " << staInt.top() << endl;while (!staInt.empty()) {cout << staInt.top() << " ";staInt.pop(); // 栈顶的元素出栈}cout << endl;system("pause");return 0;
}

六、代码执行流程

  1. 使用deque作为底层容器构造栈
  2. 依次压入1、2、3(注释掉了pop操作)
  3. 演示通过引用修改栈顶元素
  4. 输出修改后的栈顶值
  5. 遍历并清空栈

输出结果:

staInt.top: 66
staInt.size: 3
staInt.top: 88
88 2 1 

七、注意事项

  1. 空栈操作防护:执行top()pop()前必须检查empty()
  2. 元素生命周期:存储对象时注意引用有效性
  3. 容器选择原则:根据操作频率选择最优底层容器
  4. 异常安全:修改栈顶元素时需考虑可能引发的异常
http://www.xdnf.cn/news/9150.html

相关文章:

  • IoT/HCIP实验-1/物联网开发平台实验Part1(快速入门,MQTT.fx对接IoTDA)
  • 大型三甲医院更换HIS系统全流程分析与经验考察(上)
  • 【教程】给Apache服务器装上轻量级的防DDoS模块
  • 【HarmonyOS Next之旅】DevEco Studio使用指南(二十七) -> 开发云函数
  • 基于 SpringBoot 与 VueJS 的智慧就业服务平台构建:技术融合与实践创新
  • jsAPI:Intl.DateTimeFormat 属性含义
  • 尚硅谷redis7 37-39 redis持久化之AOF简介
  • Vuex 模块化和命名空间:管理大型应用的状态
  • 【MYSQL】Linux下安装mysql 8,rpm包方式安装(保姆篇)
  • 【数据插入最大值后】2022-2-2
  • 【Redis】第2节|Redis基本数据类型
  • 信息学奥赛一本通 1547:【 例 1】区间和
  • 算法-全排列
  • 怎么预测体育比赛的胜率?
  • 曲线匹配,让数据点在匹配数据的一侧?
  • 第12次06 :用户中心添加邮箱
  • 【01】大模型原理与API使用
  • 【本地面板公网访问】本地面板也能公网访问?CasaOS+1Panel+cpolar保姆级教程
  • GeoServer样式设置:使用本地图标及分层/分视野显示
  • linux中使用make clean重新编译
  • 3dmax直接导入导出gltf/glb格式插件(免费)
  • 链表面试题10之随机链表的复制
  • Windows环境下Redis的安装使用与报错解决
  • DeepSpeed-Ulysses:支持极长序列 Transformer 模型训练的系统优化方法
  • 技术视界 | 打造“有脑有身”的机器人:ABC大脑架构深度解析(上)
  • Redisson使用分布锁的详解
  • LTC之管理线索:企业抢占市场先机的制胜法宝
  • 第7章 C控制语句:分支和跳转
  • AI赋能天气预测:微软 Aurora 模型
  • 工业视觉阈值技术圣经:VisionMaster六维算法解析+脑图攻防手册