一个概念:
C++ STL - stack
代码:
#include<iostream> #include<stack>//头文件using namespace std; int main() { stack <int> stk;//创建一个栈,名为stk,元素类型为int cout<<"1: push 2: pop 3:size of zhan 4: the top number 5:empty?"<<endl; char ch; paji: cin>>ch; switch(ch) { case ‘1‘: { //元素进栈 cout<<"The number of numbers to insert:"; int num; cin>>num; while(num--) { int a; cin>>a; stk.push(a); } break; } case ‘2‘: { //使栈顶部的元素出栈,返回void类型 cout<<"The number of numbers to be stacked:"; int k; cin>>k; while(k--) { stk.pop(); } break; } case ‘3‘: { //返回栈的大小,有多少元素 cout << stk.size() << endl; break; } case ‘4‘: { //top() 返回栈最顶部的元素,且不改变栈 cout << stk.top() << endl; break; } case ‘5‘: { //查看栈是否为空,如果为空,返回真(1) cout << stk.empty() << endl; break; } } goto paji; return 0; }
今天也是元气满满的一天!good luck!
时间: 2024-10-13 07:02:56