我正在尝试为大学编写程序。该计划的目标是制定医院的护士时间表。但是,我现在真的很困。您可以在下面找到该程序的一项功能。

该功能的输入是一个名册,其中包括每个护士每天必须执行的轮班。在此示例中,我们有32行(32名护士)和28列(代表28天)。每个单元格包含一个从0到6的数字,表示休息日(0)或一定的班次(1到6)。

该功能应计算每天安排某班次安排多少名护士。例如,在第一天,有8位护士执行2班,6班3班,依此类推。该函数的输出是双精度向量。

我认为该功能大部分是正确的,但是当我针对不同的花名册调用该程序时,程序总是给出第一个花名册。

void calculate_nbr_nurses_per_shift(vector<vector<int>> roster1)
{
    for (int i = 0; i < get_nbr_days(); i++)
    {
        vector<int> nurses_per_shift;
        int nbr_nurses_free = 0;
        int nbr_nurses_shift1 = 0;
        int nbr_nurses_shift2 = 0;
        int nbr_nurses_shift3 = 0;
        int nbr_nurses_shift4 = 0;
        int nbr_nurses_shift5 = 0;
        int nbr_nurses_shift6 = 0;

        for (int j = 0; j < get_nbr_nurses(); j++)
        {
            if (roster1[j][i] == 0)
                nbr_nurses_free += 1;
            if (roster1[j][i] == 1)
                nbr_nurses_shift1 += 1;
            if (roster1[j][i] == 2)
                nbr_nurses_shift2 += 1;
            if (roster1[j][i] == 3)
                nbr_nurses_shift3 += 1;
            if (roster1[j][i] == 4)
                nbr_nurses_shift4 += 1;
            if (roster1[j][i] == 5)
                nbr_nurses_shift5 += 1;
            if (roster1[j][i] == 6)
                nbr_nurses_shift6 += 1;
        }

        nurses_per_shift.push_back(nbr_nurses_shift1);
        nurses_per_shift.push_back(nbr_nurses_shift2);
        nurses_per_shift.push_back(nbr_nurses_shift3);
        nurses_per_shift.push_back(nbr_nurses_shift4);
        nurses_per_shift.push_back(nbr_nurses_shift5);
        nurses_per_shift.push_back(nbr_nurses_shift6);
        nurses_per_shift.push_back(nbr_nurses_free);
        nbr_nurses_per_shift_per_day.push_back(nurses_per_shift);
    }
}


在这里您可以看到该程序:
Get_shift_assignment()和schedule_LD是其他花名册。

void test_schedule_function()
{
    calculate_nbr_nurses_per_shift(schedule_LD);
    calculate_nbr_nurses_per_shift(get_shift_assignment());
    calculate_coverage_deficit();
}


您需要完全了解问题的另一个功能是:

void calculate_coverage_deficit()
{
    int deficit = 0;

    for (int i = 0; i < get_nbr_days(); i++)
    {
        vector<int> deficit_day;

        for (int j = 0; j < get_nbr_shifts(); j++)
        {
            deficit = get_staffing_requirements()[j] - nbr_nurses_per_shift_per_day[i][j];
            deficit_day.push_back(deficit);
        }

        nurses_deficit.push_back(deficit_day);
    }

    cout << "Day 1, shift 1: there is a deficit of " << nurses_deficit[0][0] << " nurses." << endl;
    cout << "Day 1, shift 2: there is a deficit of " << nurses_deficit[0][1] << " nurses." << endl;
    cout << "Day 1, shift 3: there is a deficit of " << nurses_deficit[0][2] << " nurses." << endl;
    cout << "Day 1, shift 4: there is a deficit of " << nurses_deficit[0][3] << " nurses." << endl;
}


所以问题是,每次我运行该程序时,总是给我第一名单的不足。在这种情况下,这是Schedule_LD。当我第一次使用输入名册get_shift_assignment()运行该函数时,他给了我该名册的不足。
显然,第二次我运行该函数时,nbr_nurses_per_shift_per_day [] []向量没有被覆盖,而且我不知道如何解决此问题。任何帮助将不胜感激。

最佳答案

让我尝试总结一下评论:

通过使用全局变量从函数中返回值,很可能您忘记了在再次调用函数之前从一个或多个全局变量中删除较旧的结果。

要解决此问题,请改为从函数返回结果。

例如:

vector<vector<int>> calculate_nbr_nurses_per_shift(vector<vector<int>> roster1)
{
  vector<int> nbr_nurses_per_shift_per_day; // Create the result vector

  ... // Do your calculations

  return nbr_nurses_per_shift_per_day;
}


或者如果您不想返回向量:

void calculate_nbr_nurses_per_shift(vector<vector<int>> roster1, vector<vector<int>> nbr_nurses_per_shift_per_day)
{

  ... // Do your calculations

}


但是很明显,第一个变体的错误发生率要低得多(在第二个示例中,您可能会忘记再次清除nbr_of_nurses),并且大多数编译器都会优化return nbr_nurses_per_shift_per_day,因此不会复制整个向量。

第二个可能的问题是“ get_nbr_days()”可能返回大于或小于向量实际大小的数字。若要解决此问题,请使用size()vector方法或改为使用迭代器。

您的第一个函数将如下所示:

vector<vector<int>> calculate_nbr_nurses_per_shift(vector<vector<int>> roster1)
{
  vector<vector<int>> nbr_nurses_per_shift_per_day;
  for (vector<vector<int>>::iterator shiftsOnDay = roster1.begin(); shiftsOnDay != roster1.end(); ++shiftsOnDay)
  {
    vector<int> nurses_per_shift(6, 0); // Create vector with 6 elements initialized to 0
    for (vector<int>::iterator shift = shiftsOnDay->begin(); shift != shiftsOnDay->end(); ++shift)
    {
      if (*shift == 0)
        nurses_per_shift[5]++;
      else
        nurses_per_shift[*shift - 1]++; // This code relies on shift only containing meaningful values
    }
    nbr_nurses_per_shift_per_day.push_back(nurses_per_shift);
  }
  return nbr_nurses_per_shift_per_day;
}

关于c++ - vector 不能被覆盖,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20267490/

10-11 18:31