Arrays 为什么循环变量说;i';显示数组[i]处的垃圾值,而该变量的硬编码值don';T
我试图对结构数组进行排序,然后显示排序后的值。当我在循环中使用某个变量时,它会显示第一个结构的垃圾值,而其余的值与它们一样,但当我使用变量值时,它会正确显示所有值。 我的代码是:Arrays 为什么循环变量说;i';显示数组[i]处的垃圾值,而该变量的硬编码值don';T,arrays,vector,Arrays,Vector,我试图对结构数组进行排序,然后显示排序后的值。当我在循环中使用某个变量时,它会显示第一个结构的垃圾值,而其余的值与它们一样,但当我使用变量值时,它会正确显示所有值。 我的代码是: void classificationClass::sortMyStruct(location *temp) { const int loopLimit = _tempVec.size()*_tempVec[0].size(); double *_temp = (double*)malloc(sizeo
void classificationClass::sortMyStruct(location *temp)
{
const int loopLimit = _tempVec.size()*_tempVec[0].size();
double *_temp = (double*)malloc(sizeof(double));
int *_tempX = (int*)malloc(sizeof(int)), *_tempY = (int*)malloc(sizeof(int));
for (int a = 0; a < loopLimit; a++)
{
for (int b = 0; b< loopLimit; b++)
{
if (temp[b].value > temp[b+1].value)
{
*_temp = temp[b].value;
*_tempX = temp[b].xLoc;
*_tempY = temp[b].yLoc;
temp[b].value = temp[b+1].value;
temp[b].xLoc = temp[b+1].xLoc;
temp[b].yLoc = temp[b+1].yLoc;
temp[b+1].value = *_temp;
temp[b+1].xLoc = *_tempX;
temp[b+1].yLoc = *_tempY;
}
}
}
free(_temp);
free(_tempX);
free(_tempY);
}
void classificationClass::SortSystemStruct(位置*temp)
{
常量int loopLimit=_tempVec.size()*_tempVec[0].size();
double*_temp=(double*)malloc(sizeof(double));
int*_tempX=(int*)malloc(sizeof(int)),*_tempY=(int*)malloc(sizeof(int));
for(int a=0;a温度[b+1].值)
{
*_温度=温度[b]。数值;
*_tempX=temp[b].xLoc;
*_tempY=temp[b].yLoc;
温度[b]。值=温度[b+1]。值;
温度[b].xLoc=temp[b+1].xLoc;
温度[b].yLoc=temp[b+1].yLoc;
温度[b+1]。值=*\U温度;
温度[b+1].xLoc=*_tempX;
温度[b+1].yLoc=*_温度;
}
}
}
自由温度;
免费(_tempX);
免费的(临时的);
}
现在,如果我使用“17”而不是“loopLimit”,我会得到正确的值。
如果有人能给我一个正确的方向来解决这个问题,我将非常感激。我已经坚持了两三天了。
感谢当您在循环的最后一次迭代时,此代码将无效:
temp[b+1]
因为b+1
超出了数组的边界。当您在循环的最后一次迭代时,此代码将无效:temp[b+1]
因为b+1
超出了数组的边界。temp[b+1]
刚刚应用了您要求的内容,现在就成功了。感谢一位乐透的朋友,我已经把它写进了一个答案:)事实上,我写的是btemp[b+1]
刚刚应用了你的要求,它现在就起作用了。多谢了,伙计,我已经把它写进了一个答案:)事实上,发生的是我写了b