gpt4 book ai didi

c++程序停止中间循环没有错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:23:55 25 4
gpt4 key购买 nike

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

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);

最佳答案

看看这个循环:

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--;
}
...
}

关于c++程序停止中间循环没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27341563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com