1. stack 容器基本概念
stack 是一种先进后出的数据结构 只有一个出口
不允许有遍历行为
可以判断是否为空
可以返回元素个数
2. stack 容器的常用接口
入栈 -- push()
出栈 -- pop()
返回栈顶 -- top()
判断栈是否为空 -- empty()
返回栈大小 -- size()
代码示例
#include <iostream>
using namespace std;
#include <string>
#include <stack>
#include <algorithm> //标准算法头文件
//栈stack容器接口
void test01()
{
//特点:符合先进后出的数据结构
stack<int> s;
//入栈
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
//只要栈不为空 查看栈顶 并执行出栈操作
while (!s.empty())
{
//查看栈顶元素
cout << "栈顶元素:" << s.top() << endl;
//出栈
s.pop();
}
cout << "栈的大小:" << s.size() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}