博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
STL 之栈
阅读量:6986 次
发布时间:2019-06-27

本文共 705 字,大约阅读时间需要 2 分钟。

重要的数据结构。

操作:

  1. size()                                          返回实际个数
  2. empty()                                       判断是否为空
  3. push(item)                                 压栈
  4. top()                                             返回栈顶元素
  5. pop()                                            将栈顶元素删除
  6. s1.swap(s2)                               将两个栈元素交互
  7. s1 == s1                                      判断是否相等
注:栈没有clear方法,若程序需要,可以单独编写!
示例代码:
#include 
#include
using namespace std;int main() { stack
intStack; // 压 4个元素入栈 intStack.push(16); intStack.push(8); intStack.push(20); intStack.push(3); // 取栈顶元素,并弹栈 cout << "top of intStack:" << intStack.top() << endl; intStack.pop(); cout << "top of intStack:" << intStack.top() << endl; while(!intStack.empty()) { cout << intStack.top() << " "; intStack.pop(); } cout << endl; return 0;}
运行结果:
top of intStack:3
top of intStack:20
20 8 16

转载于:https://www.cnblogs.com/wjchang/p/3671647.html

你可能感兴趣的文章
面试题之矩阵与转置矩阵相乘
查看>>
linux光盘、U盘的挂载与卸载
查看>>
linux sudo命令
查看>>
LeetCode-最长回文子串
查看>>
【HDOJ】3400 Line belt
查看>>
JVM Guide
查看>>
大数模版
查看>>
HDU4044 GeoDefense(树形dp+分组背包)
查看>>
Microsoft .Net Remoting系列专题之三:Remoting事件处理全接触
查看>>
JavaScript常用标签和方法总结
查看>>
GO语言的进阶之路-网络编程之socket
查看>>
作业—四则运算题目生成器
查看>>
第十四周翻译-《Pro SQL Server Internals, 2nd edition》
查看>>
jdbcUrl is required with driverClassName spring boot 2.0版本
查看>>
C# 关于JArray和JObject封装JSON对象
查看>>
【Visual C++】游戏开发笔记之十 基础动画显示(三) 透明动画的实现
查看>>
今目标反思
查看>>
SQL Server 备份的 8 种方法。
查看>>
SQL Server 从数据库快照还原数据库
查看>>
$(document).keydown
查看>>