队列中最后一次操作循环中的错误

时间:2022-08-25 02:25:07

This is my solution for this problem statement: http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1842

这是我对此问题陈述的解决方案:http://uva.onlinejudge.org/index.php?option = onlinejudge&page = show_problem&issue {1842

I get a run time error, though the program gives correct output when I used the debugging statement below.

我得到一个运行时错误,虽然程序在我使用下面的调试语句时提供了正确的输出。

while(!q.empty()){
        cn=0;
        while(q.front().first<=ct && cn <=n && q.front().second==cpos){
            allCarT.push_back(ct+t);
            for(i=0; i< sz(allCarT); i++) cout << allCarT[i] << "\n"; //debugging statement give correct answers
            q.pop(); //error occurs here in the last loop
            cn++;
        }

        if(cn==0 && q.front().first > ct){
            ct = q.front().first;
        }else{
            ct+=t;
            if(cpos=="left") cpos="right";
            else cpos="left";
        }
    }

Full solution here :https://ideone.com/BCU3UT

完整解决方案:https://ideone.com/BCU3UT

1 个解决方案

#1


In that code, you don't check that queue is empty:

在该代码中,您不检查该队列是否为空:

while(q.front().first<=ct && cn <=n && q.front().second==cpos){
    allCarT.push_back(ct+t);
    q.pop(); //error occurs here in the last loop
    // queue can be empty now, so you have out of bound access with q.front()
    cn++;
}

#1


In that code, you don't check that queue is empty:

在该代码中,您不检查该队列是否为空:

while(q.front().first<=ct && cn <=n && q.front().second==cpos){
    allCarT.push_back(ct+t);
    q.pop(); //error occurs here in the last loop
    // queue can be empty now, so you have out of bound access with q.front()
    cn++;
}