-2

这是我的代码,我不知道这个错误的原因是什么。

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

vector<vector<int>> readFile(char* fileName)
{
    vector < vector < int > > info;
    ifstream file(fileName);
    string line;
    while ( getline(file, line) )
    {
        vector < int > data;
        double value;
        istringstream iss(line);
        while (iss >> value)
        {
            data.push_back(value);
        }
        info.push_back(data);
    }
    return info;
}

void print2DVector(vector <vector <int> > input)
{
    for ( vector < vector < int > > :: size_type i = 0, size = input.size(); i < size; ++i)
    {
        for ( vector < int > :: size_type j = 0, length = input[i].size(); j < length; ++j)
        {
            cout << input[i][j] << " ";
        }
        cout << endl;
    }
}

vector <vector <int> > inverseVector(vector <vector <int> > input)
{
    int* arr;
    arr = new int[input[0].size()];
    vector <int>  data;
    vector <vector <int> > output;
    for ( vector < vector < int > > :: size_type i = 0, size = input.size(); i < size; ++i)
    {
        for ( vector < int > :: size_type j = 0, length = input[i].size(); j < length; ++j)
        {
            arr[input[i][j]] = j;
        }
        for ( vector < int > :: size_type j = 0, length = input[i].size(); j < length; ++j)
        {
            data.push_back(arr[j]);
        }

        output.push_back(data);
        data.clear();
    }

    return output;
}

vector<vector <int>> initializeEmptyHospitals(int quota, int count)
{
    vector<vector <int>> output;
    vector<int> data;
    for(int j=0; j<quota;j++)
        data.push_back(-1);
    for(int i=0; i<count; i++)
        output.push_back(data);

    data.clear();
    return output;
}

bool isEmpty(vector <int> hospital)
{
    for(int i=0;i<hospital.size();i++)
        if(hospital[i]==-1)
            return true;

    return false;
}

void reArrangeVector(vector <int> &hospital)
{
    int temp;
    for (int i = 1; i < hospital.size(); i++)
    {
        for (int j = 0; j < hospital.size()- i; j++)
        {
            if(hospital[j]>=0 && hospital[j+1]>=0)
            if(hospital[j] > hospital[j+1])
            {
                temp = hospital[j+1];
                hospital[j+1] = hospital[j];
                hospital[j] = temp;
            }
        }
    }
}

void assignResident( vector <int> residentChoices ,vector < vector <int> > &hospitalsWithQuotas, int residentNumber, vector < vector <int> > inverseHospitals)
{
    for(int i=0;i<residentChoices.size();i++)
    {
        if(isEmpty(hospitalsWithQuotas[residentChoices[i]]))
        {
            for(int j=0; j<hospitalsWithQuotas[residentChoices[i]].size(); j++)
                if(hospitalsWithQuotas[residentChoices[i]][j] == -1)
                {
                    hospitalsWithQuotas[residentChoices[i]][j]=residentNumber;
                    reArrangeVector(hospitalsWithQuotas[residentChoices[i]]);
                    return;
                }   
        }
        else
        {
            for(int k=0; k<hospitalsWithQuotas[residentChoices[i]].size(); k++)
            {
                if(inverseHospitals[residentChoices[i]][residentNumber] < hospitalsWithQuotas[residentChoices[i]][0])
                {
                    int temp = hospitalsWithQuotas[residentChoices[i]][0];
                    hospitalsWithQuotas[residentChoices[i]][0] = residentNumber;
                    assignResident(residentChoices, hospitalsWithQuotas, temp, inverseHospitals);
                    reArrangeVector(hospitalsWithQuotas[residentChoices[i]]);
                    return;
                }
                //if(k==hospitalsWithQuotas[residentChoices[i]].size()-1)
            }
        }
    }
}

int main(int argc, char* argv[])
{
    vector < vector <int> > hospitalChoices,residentChoices,inverseHospitals,hospitalsWithQuotas;
    int quota = 3;
    hospitalChoices = readFile("data1_h1.txt");
    residentChoices = readFile("data1_r1.txt");
    hospitalsWithQuotas = initializeEmptyHospitals(quota, hospitalChoices.size());  
    inverseHospitals = inverseVector(hospitalChoices);

    for(int i=0; i<15; i++)
        assignResident(residentChoices[i],hospitalsWithQuotas,i,inverseHospitals);

    print2DVector(hospitalsWithQuotas);

    system("pause");
    return 0;
}

在下面的 for 循环中,当我写 i<12 时,它可以工作,但对于 i<15,它会引发堆栈溢出异常。

for(int i=0; i<15; i++)
    assignResident(residentChoices[i],hospitalsWithQuotas,i,inverseHospitals);

整个错误文本在 VS2012 中:

Unhandled exception at 0x00CB33D9 in BLG372E HW1.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00E72F50).
4

1 回答 1

2

您的程序中的一些错误:

  1. 在 readFile 函数中,值未初始化,您正在比较它。在比较之前对其进行初始化。
  2. 在 readFile 函数中,您正在使用向量并且正在向其中推送字符,这是错误的。

您收到 Stackoverflow 错误可能是因为一些无休止的递归。

建议:在发布到任何论坛之前,请先格式化您的代码,以便其他人调试它。

于 2013-03-24T11:56:20.710 回答