C++ 数组中最近点的索引,每个点包含3个元素

C++ 数组中最近点的索引,每个点包含3个元素,c++,algorithm,C++,Algorithm,我试图找到一个数组(pts)中最近点的索引,每个数组包含3个元素。 我已经编写了以下代码,但它没有返回最近点的索引:-如果有人能告诉我我在哪里,我将不胜感激。我错了。谢谢 void point_index(Point pts[], int &size) } cout << x << " " << y << endl; void point_索引(point pts[],int&size) } cout一旦找到最小值,就需要指定

我试图找到一个数组(pts)中最近点的索引,每个数组包含3个元素。 我已经编写了以下代码,但它没有返回最近点的索引:-如果有人能告诉我我在哪里,我将不胜感激。我错了。谢谢

void point_index(Point pts[], int &size)



}
cout << x << "   " <<  y << endl;
void point_索引(point pts[],int&size)
}

cout一旦找到最小值,就需要指定x、y和min。 请尝试以下代码:

double a;
int x = 0, y = 1;

double min = get_distance(pts[x],pts[y]);
for (int i = 0; i < size; i++) 
{
    for (int j = i+1; j < size; j++) 
    {
        double d = get_distance(pts[i], pts[j]);
        if (d < min) 
        {
            x = i;
            y = j;
            min = d;
        }
    }
}
cout << x << "   " <<  y << endl;
双a;
int x=0,y=1;
双最小值=获取距离(pts[x],pts[y]);
对于(int i=0;icout您没有将最小值存储在
min
中,比较后,将最小值存储在
min
中,如果要正确比较,则将距离值设置在外部

a = get_distance(pts[i],pts[j]);
if (a < min) {
     x = i;
     y = j;
     min = a;
}
a=get_距离(pts[i],pts[j]);
如果(a
在初始化之前,您正在使用
a
。此外,
j
增量是无用的,并且您的函数不会返回任何东西为什么不
double a,min=get_distance(pts[0],pts[1])然后<代码>用于。。。对于如果((a=get_距离(pts[i],pts[j])
(另外,您不能从type
void
返回任何内容,您的意思是
Point Point\u index(…)
?您需要保留一个temp
Point
,并将其设置为最小索引,然后返回该值)请提供.@DavidC.Rankin我已更新了上面的代码,但它仍然没有打印最近点的索引。@DavidC.Rankin感谢您的帮助!成功了!干得好,很高兴有帮助。