stack的用法

点击查看代码
#include<cstdio>
#include<stack>
#pragma warning(disable:4996)
using namespace std;

int main() {
	stack<int> st;
	for (int i = 1; i <= 5; i++) {
		st.push(i); //将i压入栈中,st:1 2 3 4 5
	}
	printf("%d\n", st.top()); //只能使用top()访问栈中元素,返回栈顶元素的值5	
	while (st.empty() == false) { //st栈内还有元素,就继续弹出栈顶元素
		st.pop(); //弹出栈顶元素,即删除栈顶元素
	}
	printf("%d\n", st.size()); //st清空后,元素个数变为0

	return 0;
}
posted @ 2022-09-30 23:04  zhaoo_o  阅读(16)  评论(0编辑  收藏  举报