題目:將一個棧逆序
思路:新建一個棧,將棧中的元素輸出到新的棧,則棧中元素逆序。
知識點:棧的基本操作
push:入棧
pop:出棧
top:取棧頂元素
size:獲取棧的大小
empty:檢測棧是否爲空
#include<iostream>
#include<stack>
using namespace std;
int main() {
stack <int> stk1;
stack <int> stk2;
for (int i = 0; i < 50; i++) {
stk1.push(i);
}
while (!stk1.empty())
{
int top = stk1.top();
stk1.pop();
stk2.push( top );
}
cout << "棧的大小 " << stk2.size() << endl;
while (!stk2.empty()) {
cout << stk2.top() << endl;
stk2.pop();
}
system("pause");
}
注意事項:push有參數,pop、size、empty都沒有參數。