- 实现代码(STL)
// -*- coding: utf-8 -*-
// @ Date : 2021/5/20 13:14
// @ Author : RichardLau_Cx
// @ file : Richard.cpp
// @ IDE : Dev-C++
// @ Source : Data_Structure
/**
* C++中针对栈的基本操作
*/
#include <iostream>
#include <stack>
using namespace std;
int main() {
// 1. 初始化构造
stack<int> st;
// 2. 进栈操作
st.push(4);
st.push(1);
st.push(3);
st.push(1);
st.push(0);
st.push(2);
st.push(5);
// 3. 输出元素数量
cout << " size: " << st.size();
// 4. 输出栈顶元素
cout << " - top: " <<st.top() << endl << endl;
// 5. 判断栈空
while (!st.empty())
{
cout << "\t" << st.size() << " - " << st.top() << endl;
// 6. 出栈操作
st.pop();
}
return 0;
}