1.定义
stack是一种先进后出的数据结构,他只有一个接口。
栈只有最顶端的元素可以使用,因此栈不允许有遍历行为。
因为进行遍历操作时,会有出栈行为,改变了本来的结构。
(改变容器本身结构的行为不叫作遍历)
2.常用接口
1.函数原型
2.代码实现
#include<stdio.h>
using namespace std;
#include<stack>
#include<algorithm>
#include <iostream>
int main()
{
stack<int> a;
//入栈
a.push(10);
a.push(20);
a.push(30);
a.push(40);
while (!a.empty())
{
cout << "栈顶元素:" << a.top() << endl;
//出栈
a.pop();
}
cout << "栈的大小"<<a.size() << endl;
return 0;
}