STL 之栈

38 篇文章 0 订阅

目录


重要的数据结构。

操作:

  1. size()                                          返回实际个数
  2. empty()                                       判断是否为空
  3. push(item)                                 压栈
  4. top()                                             返回栈顶元素
  5. pop()                                            将栈顶元素删除
  6. s1.swap(s2)                               将两个栈元素交互
  7. s1 == s1                                      判断是否相等
注:栈没有clear方法,若程序需要,可以单独编写!
示例代码:
#include <stack>
#include <iostream>

using namespace std;

int main() {
	stack<int> 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

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yours风之恋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值