stack即为栈,是STL中实现一个后进先出的容器。使用时需要加上#include<stack>
头文件。
1.stack的定义
stack<int> a;
stack<char> b;
stack<double> c;
2.stack元素访问
在STL中只能通过top()来访问栈顶元素。
stack<int> a;
for(int i=1;i<=5;i++){
a.push(i); //将i入栈
}
printf("%d",a.top()); //输出5
3.stack常用函数
(1) push()
push(x)将x入栈。
(2) top()
top()获得栈顶元素。
(3) pop()
pop()用来弹出(移除)栈顶元素。
(4) empty()
empty()检测stack是否为空,若为空则为true,若为非空则为false。
(5) size()
size()返回stack元素内的个数。
网友评论