【问题标题】:i'm getting "timeout: the monitored command dumped core" error in c++ code of stack我在堆栈的 C++ 代码中收到“超时:监控的命令转储核心”错误
【发布时间】:2021-11-15 01:55:01
【问题描述】:

有 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 阿格拉 push1 斋浦尔 push0 勒克瑙 push2 博帕尔 push1 阿杰梅尔 push1 乌代浦 pop0 pop1 push2 Indore push0 Meerut stop1

我得到超时:监控的命令转储核心错误。

【问题讨论】:

    标签: c++ stack


    【解决方案1】:

    看看你的这部分代码:

    else if(a=="stop1") {
        while(!stk0.empty()) {
            cout<<stk1.top()<<endl;
            stk1.pop();
        }
        break;
    }
    

    astop1时,循环会不断循环,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;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 2021-10-10
      相关资源
      最近更新 更多