stack的用法

 1.stack是C++中的容器适配器,被设计为先进后出(FILO),元素的插入和删除只能在尾部进行

2.stack支持以下操作:


  • empty(判断是否为空,返回true为空,返回false为非空)
  • size(返回栈的元素个数)
  • top(弹出栈顶元素)
  • push(入栈)
  • pop(出栈,弹出栈顶元素)
#include<utility>
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
using namespace std;
int main()
{
	stack<int> st;
	if (st.empty())
	{
		cout << "空0" << endl;
	}

	for (int i = 1; i <= 5; i++)
	{
		st.push(i); //元素入栈
	}

	cout << "栈中元素个数:" << st.size() << endl;

	if (st.empty())
	{
		cout << "空1" << endl;
	}

	for (int i = 1; i <= 5; i++)
	{
		cout << st.top() << endl;//打印栈顶元素
		st.pop(); //出栈
	}

	cout << "栈中元素个数:" << st.size() << endl;

	if (st.empty())
	{
		cout << "空2" << endl;
	}
	
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值