算法笔记-6.7 stack用法

#include<stdio.h>
#include<stack>
using namespace std;
int main(){
	stack<int> st;
	for(int i=1;i<=5;i++){
		st.push(i);//压入栈 依次入栈为1 2 3 4 5
	}
	printf("%d\n",st.top());//top()取栈顶元素   5
	return 0;
}

/*
------------stack相当于栈 后进先出-----------
1.a.push(x)-------->x入栈
2.a.top()---------->栈顶元素
3.a.pop()---------->弹出栈顶元素
4.a.empty()-------->检测a是否为空
5.a.size()--------->a的元素个数
6.用途:用来模拟实现一些递归 防止程序对栈内存的限制而导致程序出错
*/

猜你喜欢

转载自blog.csdn.net/weixin_42127158/article/details/82017455