在队列上的最后一个操作循环中的错误
Bug in the last loop of operations on a queue
这是我解决此问题语句的解决方案:http://uva.onlinejudge.org/index.php?option = onlinejudge间
我会遇到运行时间错误,尽管当我使用下面的调试语句时,该程序给出了正确的输出。
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";
}
}
完整的解决方案:https://ideone.com/bcu3ut
在该代码中,您不检查队列是空的:
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++;
}
相关文章:
- 带有开关语句的 do-while 循环 -- 无穷循环错误
- C++ 中的类之间的数据重新循环 - 错误:'<class name>'未在此范围内声明
- 我在 c ++ 中有一个循环错误
- 无法发现嵌套的循环错误
- CPP 在读取结构数据时无限循环错误?
- 虽然循环错误:"Uninitialized local variable used"
- 如何修复关卡顺序遍历问题(二叉树)的无限循环错误
- 随机密码生成器的C++循环错误"以非零状态退出"
- <BadPtr> 循环错误
- 试图在C++中验证双精度有效输入的循环错误
- c++中的switchcase无限循环错误
- 文件IO循环错误
- for循环错误地输出指数
- 输入 Ctrl+Z 结束阅读单词将导致循环错误
- C++教授无法弄清楚的循环错误
- C++If循环错误
- 建议处理while循环错误
- 使用自动输入循环错误"auto must have an initializer"
- 输入数字时出现循环错误
- C++ "if"循环中的循环错误