【问题标题】:c++ Program stops mid loop with no errorsc ++程序停止中间循环而没有错误
【发布时间】:2015-02-05 03:19:39
【问题描述】:

我正在开发一个医院模拟程序,在该程序中,患者按严重程度分类并放置在相应的队列中。当我调试它时,一切似乎都工作正常,但由于某种原因,它在随机数量的循环后停止。我没有收到任何错误——我的程序只是退出了中间循环。什么可能导致这种情况发生?

priority_queue<Hospital_Visit> queue1, queue2;
vector<int> nurse_time_vector, doctor_time_vector;
vector<Hospital_Visit> current_hospital_visit;

for (int i = 0; i < master_hospital_visit.size(); i++){ //make a copy of the master_hospital_visit to manipulate
    current_hospital_visit.push_back(master_hospital_visit[i]);
}



if (num_nurses>0)
    nurse->set_minute(0);
if (num_doctors>0)
    doctor->set_minute(0);

int treatment;

map<string, vector<Hospital_Visit>>::iterator it;

srand(time(NULL));

bool hour_changed = true;

do{

    if (hour_changed){ //update queues with new hour's patients added to end
        for (int i = 0; i < current_hospital_visit.size(); i++){
            if (current_hospital_visit[i].get_hour() > current_hour){
                break;
            }
            if (current_hospital_visit[i].get_hour() <= current_hour)
            {
                if (current_hospital_visit[i].get_severity() <= 10) // For nurses
                {
                    queue1.push(current_hospital_visit[i]);
                    current_hospital_visit.erase(current_hospital_visit.begin() + i);
                }
                else // for doctors
                {
                    queue2.push(current_hospital_visit[i]);
                    current_hospital_visit.erase(current_hospital_visit.begin() + i);
                }
            }
        }
    }

    hour_changed = false;


        if (nurse->get_minute() != nurse->get_max_time() && queue1.size() != 0 && queue1.top().get_hour() <= current_hour && num_nurses > 0)
        {
            treatment = (rand() % 11) + 1;

            queue1.top().set_treatment_time(treatment);

            queue1.top().set_day(current_hour / 24);

            queue1.top().set_hour(current_hour);

            nurse->set_minute(treatment);

            queue1.top().set_wait_time(current_hour - queue1.top().get_hour() + treatment);

            queue1.top().set_medic(false);


            queue1.top().set_severity(queue1.top().get_severity() - (current_hour - queue1.top().get_hour()));


            it = patients_map.find(queue1.top().get_name());
            if (it == patients_map.end())
            {
                vector<Hospital_Visit> patient_visits;

                patient_visits.push_back(queue1.top());

                patients_map.insert(make_pair(queue1.top().get_name(), patient_visits));
            }
            else
            {
                it->second.push_back(queue1.top());
            }
            queue1.pop();
        }
        else if (queue1.size() == 0 && num_nurses > 0) //was !=
        {
            nurse_time_vector.push_back(nurse->get_max_time() - nurse->get_medic_hour());
        }

        if (doctor->get_medic_hour() != doctor->get_max_time() && queue2.size() != 0 &&
            queue2.top().get_hour() <= current_hour && num_doctors > 0)
        {
            treatment = (rand() % 21) + 1;

            queue2.top().set_treatment_time(treatment);

            queue2.top().set_day(current_hour / 24);

            queue2.top().set_hour(current_hour);

            doctor->set_minute(treatment);

            queue2.top().set_wait_time(current_hour - queue2.top().get_hour() + treatment);

            queue2.top().set_medic(true);

            queue2.top().set_severity(queue2.top().get_severity() - (current_hour - queue2.top().get_hour()));

            it = patients_map.find(queue2.top().get_name());
            if (it == patients_map.end())
            {

                vector<Hospital_Visit> patient_visits;

                patient_visits.push_back(queue2.top());

                patients_map.insert(make_pair(queue2.top().get_name(), patient_visits));
            }
            else
            {

                it->second.push_back(queue2.top());
            }
            queue2.pop();
        }

        else if (doctor->get_minute() != doctor->get_max_time() && queue1.size() != 0 && queue2.size() == 0
            && queue1.top().get_hour() <= current_hour && num_doctors > 0){

            treatment = (rand() % 21) + 1;

            queue1.top().set_treatment_time(treatment);

            queue1.top().set_day(current_hour / 24);

            queue1.top().set_hour(current_hour);

            doctor->set_minute(treatment);

            queue1.top().set_wait_time(current_hour - queue1.top().get_hour() + treatment);

            queue1.top().set_medic(true);

            queue1.top().set_severity(queue1.top().get_severity() - (current_hour - queue1.top().get_hour()));

            it = patients_map.find(queue1.top().get_name());
            if (it == patients_map.end())
            {
                vector<Hospital_Visit> patient_visits;
                patient_visits.push_back(queue1.top());
                patients_map.insert(make_pair(queue1.top().get_name(), patient_visits));
            }
            else
            {
                it->second.push_back(queue1.top());
            }
            queue1.pop();

        }

        if ((doctor->get_minute() <= doctor->get_max_time() + 20 && doctor->get_minute() >= doctor->get_max_time()-5) 
            || (queue1.size() == 0 && queue2.size() == 0))
            { // if the max time is reached or the queues are empty, then change the hour

            current_hour++;
            hour_changed = true;

            if (num_nurses>0)
                nurse->set_minute(0);
            if (num_doctors>0)
                doctor->set_minute(0);

            }

} while (current_hour != 169);

【问题讨论】:

  • 这是学习如何使用调试器的完美环境。
  • 过去 3 小时我一直在调试它。它没有给我任何错误,甚至没有说程序退出。它只是退出 - 即调试器停止。
  • 如果您想要重现性,请为您的 PRNG 播种一个恒定值,或者根本不播种。然后在调试器中运行。如果普通运行没有显示错误在哪里,请逐步跟踪执行,以找到有问题的行。你的问题是无法回答的:太多的代码无法通过目测发现错误,但不足以重现它。
  • 它到底停在哪里?哪条线?总是在同一行吗?
  • 我先添加一些异常处理。

标签: c++ loops console-application simulator terminate


【解决方案1】:

看看这个循环:

for (int i = 0; i < current_hospital_visit.size(); i++){
    ...
    if (...)
    {
        current_hospital_visit.erase(current_hospital_visit.begin() + i);
    }
    ...
}

它不会检查每个元素。假设i3,并且if 条件为真。元素3 被删除,使元素4 成为新元素3。但是在下一次迭代中,i 会递增并变为4,因此这个新元素3 将永远不会被测试。

一个可能的解决方案是在您删除某些内容时减少 i

for (int i = 0; i < current_hospital_visit.size(); i++){
    ...
    if (...)
    {
        current_hospital_visit.erase(current_hospital_visit.begin() + i);
        i--;
    }
    ...
}

【讨论】:

  • 我的意图是制作一个医院就诊向量,然后在该向量中搜索等于或低于“当前时间”的项目,然后将它们添加到治疗队列并从向量中删除它们,以便下一次增加小时时没有重复。这个逻辑有意义吗,还是我应该尝试一些不同的东西?
  • 是的,这是有道理的,只是不是所有等于或超过当前时间的项目都将使用当前代码移动。例如,您可以在每次移动元素时写入 i--
  • 哦,我现在明白你的意思了。我试试看!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多