簡體   English   中英

在c ++中使用排序選擇和結構數組

[英]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也是一個指針。 scoresdouble*類型[恰好可以指向大小為NUM_SCORES的數組]您正在為int指定一個double指針。

你試圖將double *(雙精度數組是double *)分配給double。

你可以試着改變你的double型數組一個vector雙打。 然后你可以使用std::sortstd::stable_sort對它們進行std::sort

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM