gpt4 book ai didi

c++ - 在 C++ 中对结构数组使用排序选择

转载 作者:行者123 更新时间:2023-11-28 03:18:22 26 4
gpt4 key购买 nike

我不知道如何让排序选择起作用。我必须按升序对结构中的分数( double )进行排序。

这是我的代码,我会在出现错误的地方进行评论。

我的结构:

struct diveInfo   
{
string diversName;
double totalScore;
double totalScore;
double diff;
double scores[NUM_SCORES];
};

我按升序对分数进行排序的函数:

void selectionSort(diveInfo *ptr,  int size)
{
diveInfo temp;

double minValue;

int startScan;
int minIndex;

for ( startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = ptr[startScan].scores; //keep getting an error here saying type double cannot be assigned to an entity of type double.
temp = ptr[startScan];

for (int index = startScan + 1; index < size; index++)
{
if ( ptr[index].scores < minValue)
{
temp = ptr[index];
minIndex = index;
}

}
ptr[minIndex] = ptr[startScan];
ptr[startScan] = temp;
}
}

最佳答案

scores 是 double 组,您需要在该数组中指定索引才能访问特定的 double 值。
例如 minValue = ptr[startScan].scores[0];

关于c++ - 在 C++ 中对结构数组使用排序选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16194357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com