stack基本概念:
概念:stack是一种先进后出的数据结构,他只有一个出口
栈中只有顶端的元素才可以被外界使用,因此栈不允许有遍历行为
栈中进入数据称为————入栈 push
栈中弹出数据称为————出栈 pop
入栈———push
出栈———pop
返回栈顶———top
判断栈是否为空———empty
返回栈顶大小———size
#include<iostream>
using namespace std;
#include<stack>
void test01()
{
stack<int>s;
//入栈
s.push(10);
s.push(20);
s.push(30);
s.push(40);
//只要栈不为空,查看栈顶,并且执行出栈操作
while (!s.empty())
{
//查看栈顶元素
cout << "栈顶元素为:" << s.top() << endl;
//出栈
s.pop();
}
cout << "栈中中大小:" << s.size() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}