我在堆栈的 C++ 代码中遇到 "timeout: the monitored command dumped core" 错误
i'm getting "timeout: the monitored command dumped core" error in c++ code of stack
有 3 个堆栈 stk0、stk1 和 stk2。为了区分 push 和 pop,程序使用 push0、push1 和 push2 作为 3 个堆栈,同样使用 pop0、pop1 和 pop2。程序以 stop0、stop1 或 stop2 结束,并显示 stack0、stack1 或 stack2 的内容,然后退出。我的代码适用于所有测试用例接受我在下面提到的那个。
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<string> stk0;
stack<string> stk1;
stack<string> stk2;
while(true) {
string a;
cout<<"Give one of options: pop, push, stop\n";
cin >> a;
if(a=="push0") {
string b;
cin >> b;
stk0.push(b);
}
else if(a=="push1") {
string b;
cin >> b;
stk1.push(b);
}
else if(a=="push2") {
string b;
cin >> b;
stk2.push(b);
}
else if(a=="pop0") {
if(!stk0.empty()) {
string b = stk0.top();
stk0.pop();
cout<<"Element popped from stack 0 is: "<<b<<endl;
}
else cout<<"Underflow in stack 0\n";
}
else if(a=="pop1") {
if(!stk1.empty()) {
string b = stk1.top();
stk1.pop();
cout<<"Element popped from stack 1 is: "<<b<<endl;
}
else cout<<"Underflow in stack 1\n";
}
else if(a=="pop2") {
if(!stk2.empty()) {
string b = stk2.top();
stk2.pop();
cout<<"Element popped from stack 2 is: "<<b<<endl;
}
else cout<<"Underflow in stack 2\n";
}
else if(a=="stop0") {
while(!stk0.empty()) {
cout<<stk0.top()<<endl;
stk0.pop();
}
break;
}
else if(a=="stop1") {
while(!stk0.empty()) {
cout<<stk1.top()<<endl;
stk1.pop();
}
break;
}
else if(a=="stop2") {
while(!stk2.empty()) {
cout<<stk2.top()<<endl;
stk2.pop();
}
break;
}
}
}
当我输入
push0 Agra push1 Jaipur push0 Lucknow push2 Bhopal push1 Ajmer push1
Udaipur pop0 pop1 push2 Indore push0 Meerut stop1
我得到超时:监控命令转储核心错误。
查看您的这部分代码:
else if(a=="stop1") {
while(!stk0.empty()) {
cout<<stk1.top()<<endl;
stk1.pop();
}
break;
}
当a
为stop1
时,循环会不断循环,弹出stk1
并打印其值,但不会退出循环,因为条件是!stk0.empty()
而不是 !stk1.empty()
.
您可能想这样做:
else if(a=="stop1") {
while(!stk1.empty()) { // <- Notice change from 'stk0.empty()' to 'stk1.empty()'
cout<<stk1.top()<<endl;
stk1.pop();
}
break;
}
有 3 个堆栈 stk0、stk1 和 stk2。为了区分 push 和 pop,程序使用 push0、push1 和 push2 作为 3 个堆栈,同样使用 pop0、pop1 和 pop2。程序以 stop0、stop1 或 stop2 结束,并显示 stack0、stack1 或 stack2 的内容,然后退出。我的代码适用于所有测试用例接受我在下面提到的那个。
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<string> stk0;
stack<string> stk1;
stack<string> stk2;
while(true) {
string a;
cout<<"Give one of options: pop, push, stop\n";
cin >> a;
if(a=="push0") {
string b;
cin >> b;
stk0.push(b);
}
else if(a=="push1") {
string b;
cin >> b;
stk1.push(b);
}
else if(a=="push2") {
string b;
cin >> b;
stk2.push(b);
}
else if(a=="pop0") {
if(!stk0.empty()) {
string b = stk0.top();
stk0.pop();
cout<<"Element popped from stack 0 is: "<<b<<endl;
}
else cout<<"Underflow in stack 0\n";
}
else if(a=="pop1") {
if(!stk1.empty()) {
string b = stk1.top();
stk1.pop();
cout<<"Element popped from stack 1 is: "<<b<<endl;
}
else cout<<"Underflow in stack 1\n";
}
else if(a=="pop2") {
if(!stk2.empty()) {
string b = stk2.top();
stk2.pop();
cout<<"Element popped from stack 2 is: "<<b<<endl;
}
else cout<<"Underflow in stack 2\n";
}
else if(a=="stop0") {
while(!stk0.empty()) {
cout<<stk0.top()<<endl;
stk0.pop();
}
break;
}
else if(a=="stop1") {
while(!stk0.empty()) {
cout<<stk1.top()<<endl;
stk1.pop();
}
break;
}
else if(a=="stop2") {
while(!stk2.empty()) {
cout<<stk2.top()<<endl;
stk2.pop();
}
break;
}
}
}
当我输入
push0 Agra push1 Jaipur push0 Lucknow push2 Bhopal push1 Ajmer push1 Udaipur pop0 pop1 push2 Indore push0 Meerut stop1
我得到超时:监控命令转储核心错误。
查看您的这部分代码:
else if(a=="stop1") {
while(!stk0.empty()) {
cout<<stk1.top()<<endl;
stk1.pop();
}
break;
}
当a
为stop1
时,循环会不断循环,弹出stk1
并打印其值,但不会退出循环,因为条件是!stk0.empty()
而不是 !stk1.empty()
.
您可能想这样做:
else if(a=="stop1") {
while(!stk1.empty()) { // <- Notice change from 'stk0.empty()' to 'stk1.empty()'
cout<<stk1.top()<<endl;
stk1.pop();
}
break;
}