导航
1.stack基本知识
———————————————————————————————————
1.stack基本知识
概念:是一种先进后出的数据结构,只有一个出口
栈不允许遍历的行为
构造函数
stack< int> stk
stack(const stack& stk)
赋值操作
stack& operator=(const stack& stk)
数据存取
push(elem) //入栈
pop() //出栈
top() //查看栈顶
size() //查看栈中元素
例子:
#include <iostream>
using namespace std;
#include <stack>
//stack栈容器:先进后出
void test()
{
stack<int> stk;
//入栈
stk.push(10);
stk.push(20);
stk.push(30);
stk.push(40);
cout<<"栈的大小:"<<stk.size()<<endl;
//只要栈不为空,查看栈顶,并且执行出栈操作
while(!stk.empty())
{
cout<<"栈顶元素为:"<<stk.top()<<endl;
//出栈
stk.pop();
}
cout<<"栈的大小:"<<stk.size()<<endl;
}
int main()
{
test();
system("pause");
return 0;
}
运行结果: