我是使用 c++ 的新手,所以这可能是一个向量问题。我收到一个浮点错误。我不认为这是除以零错误。任何帮助,将不胜感激。
代码可以编译,但是运行时会弹出错误。“浮点异常(核心转储)”
vector<int> radixSort (vector<int> &numbs, int maxA)
{
vector<vector<int> >temp1;
vector<vector<int> >temp2;
for (int i = 0; i < 10 ; i++)
{
temp1.push_back(vector<int>());
temp2.push_back(vector<int>());
}
int x = 1;
int plc = 0;
while (maxA/x > 0)
{
plc = plc + 1;
x = x * 10;
}
for (int i = 0; i < (int)numbs.size(); i++)
{
int aryLoc = numbs[i] % 10;
temp1[aryLoc].push_back(numbs[i]);
}
plc = plc - 1;
int modDiv = 100;
int intDiv = 10;
bool isTemp1 = true;
while (plc > 0)
{
for (int i = 0; i < 10 ; i++)
{
if (temp1[i].size() > 0)
{
for (int j = 0; j < temp1[i].size() ; j++)
{
int aryLoc = temp1[i][j] % modDiv;
aryLoc = temp1[i][j] / intDiv;
temp2[aryLoc].push_back(numbs[i]);
modDiv = modDiv * 10;
intDiv = intDiv * 10;
}
}
}
plc = plc - 1;
isTemp1 = false;
for (int i = 0; i < 10 ; i++)
{
temp1[i].clear();
}
if (plc > 0)
{
for (int i = 0; i < 10 ; i++)
{
if (temp2[i].size() > 0)
{
for (int j = 0; j < temp2[i].size() ; j++)
{
int aryLoc = temp2[i][j] % modDiv;
aryLoc = temp2[i][j] / intDiv;
temp1[aryLoc].push_back(numbs[i]);
modDiv = modDiv * 10;
intDiv = intDiv * 10;
}
}
}
plc = plc - 1;
isTemp1 = true;
for (int i = 0; i < 10 ; i++)
{
temp2[i].clear();
}
}
}
if (isTemp1 == true)
{
int y = 0;
for (int i = 0; i < 10 ; i++)
{
if (temp1[i].size() > 0)
{
for (int j = 0; j < temp1[i].size() ; j++)
{
numbs[y] = temp1[i][j];
y++;
}
}
}
}
else
{
int y = 0;
for (int i = 0; i < 10 ; i++)
{
if (temp2[i].size() > 0)
{
for (int j = 0; j < temp2[i].size() ; j++)
{
numbs[y] = temp2[i][j];
y++;
}
}
}
}
return numbs;
}
感谢您的帮助,当我发现我的错误时,现在似乎正在工作这里是固定副本
vector<int> radixSort (vector<int> &numbs, int maxA)
{
vector<vector<int> >temp1;
vector<vector<int> >temp2;
for (int i = 0; i < 10 ; i++)
{
temp1.push_back(vector<int>());
temp2.push_back(vector<int>());
}
int x = 1;
int plc = 0;
while (maxA/x > 0)
{
plc = plc + 1;
x = x * 10;
}
for (int i = 0; i < (int)numbs.size(); i++)
{
int aryLoc = numbs[i] % 10;
temp1[aryLoc].push_back(numbs[i]);
}
plc = plc - 1;
int modDiv = 100;
int intDiv = 10;
bool isTemp1 = true;
while (plc > 0)
{
for (int i = 0; i < 10 ; i++)
{
if (temp1[i].size() > 0)
{
for (int j = 0; j < temp1[i].size() ; j++)
{
int aryLoc = temp1[i][j] % modDiv;
aryLoc = aryLoc / intDiv;
temp2[aryLoc].push_back(temp1[i][j]);
}
}
}
modDiv = modDiv * 10;
intDiv = intDiv * 10;
plc = plc - 1;
isTemp1 = false;
for (int i = 0; i < 10 ; i++)
{
temp1[i].clear();
}
if (plc > 0)
{
for (int i = 0; i < 10 ; i++)
{
if (temp2[i].size() > 0)
{
for (int j = 0; j < temp2[i].size() ; j++)
{
int aryLoc = temp2[i][j] % modDiv;
aryLoc = aryLoc / intDiv;
temp1[aryLoc].push_back(temp2[i][j]);
}
}
}
modDiv = modDiv * 10;
intDiv = intDiv * 10;
plc = plc - 1;
isTemp1 = true;
for (int i = 0; i < 10 ; i++)
{
temp2[i].clear();
}
}
}
if (isTemp1 == true)
{
int y = 0;
for (int i = 0; i < 10 ; i++)
{
if (temp1[i].size() > 0)
{
for (int j = 0; j < temp1[i].size() ; j++)
{
numbs[y] = temp1[i][j];
y++;
}
}
}
}
else
{
int y = 0;
for (int i = 0; i < 10 ; i++)
{
if (temp2[i].size() > 0)
{
for (int j = 0; j < temp2[i].size() ; j++)
{
numbs[y] = temp2[i][j];
y++;
}
}
}
}
return numbs;
}