黑马:stack容器(211~212)

本文详细介绍了C++中栈的基本概念,包括先进后出的数据结构特点,并通过test01函数展示了stack容器的常用接口,如push入栈、pop出栈、top查看栈顶元素、empty判断空栈和size获取栈大小的操作。
摘要由CSDN通过智能技术生成

1.基本概念

stack是一种先进后出的数据结构,只有一个出口

 

2.常用接口

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>  //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>

void test01() {
	stack<int>s;

	//入栈
	s.push(10);
	s.push(20);
	s.push(30);
	s.push(40);

	cout << "栈的大小为: " << s.size() << endl;

	//只要栈不为空,查看栈顶,并且执行出栈操作
	while (!s.empty()) {
		//查看栈顶元素
		cout << "栈顶元素: " << s.top() << endl;

		//出栈
		s.pop();
	}

	cout << "栈的大小为: " << s.size() << endl;
}




int main(int argc,char**argv) {

	test01();
	
	system("pause");
	return  0;
}




总结:

入栈--push

出栈--pop

返回栈顶--top

判断是否为空--empty

返回栈大小--size

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值