简介:本文通过一段代码,教大家学会stack的使用。
学习代码:
#include<iostream>
#include<stack>
using namespace std;
// Stack container
void test01()
{
// Features: in line with the first in and out
// 栈的特性:栈是一种后进先出的数据结构
stack<int> s; // default contruction
// push
// 往栈里面放入元素
s.push(10);
s.push(20);
s.push(30);
s.push(40);
cout << "栈的大小:" << s.size() << endl;
// As long as the stack is not empty, check the stack and execute the stack operation
while (!s.empty())
{
// View top of stack elements
// 查看栈顶元素
cout << "栈顶元素为:" << s.top() << endl;
// pop
// 出栈
s.pop();
}
cout << "栈的大小:" << s.size() << endl;
}
int main()
{
test01();
return 0;
}
运行结果: