题目:将一个栈逆序
思路:新建一个栈,将栈中的元素输出到新的栈,则栈中元素逆序。
知识点:栈的基本操作
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都没有参数。