这是我的代码,我不知道这个错误的原因是什么。
#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).