[英]using sort selection with an array of structs in c++
我無法弄清楚如何使排序選擇工作。 我必須按結構升序對結構中的分數(雙精度)進行排序。
這是我的代碼,我會評論我在哪里得到錯誤。
我的結構:
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值。
例如minValue = ptr[startScan].scores[0];
minValue = ptr[startScan].scores;
scores
是一系列雙打。 arrayname也是一個指針。 scores
是double*
類型[恰好可以指向大小為NUM_SCORES
的數組]您正在為int指定一個double
指針。
你試圖將double *(雙精度數組是double *)分配給double。
你可以試着改變你的double型數組一個vector
雙打。 然后你可以使用std::sort
或std::stable_sort
對它們進行std::sort
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.