简体   繁体   English

在c ++中使用排序选择和结构数组

[英]using sort selection with an array of structs in c++

I can't figure out how to get the sort selection working. 我无法弄清楚如何使排序选择工作。 I have to sort the scores (doubles) from the struct in ascending order. 我必须按结构升序对结构中的分数(双精度)进行排序。

Here is my code, I will comment where i am getting the errors. 这是我的代码,我会评论我在哪里得到错误。

My struct: 我的结构:

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

My function to sort the scores in ascending order: 我的功能是按升序排序分数:

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 is array of doubles, you need to specify index in this array to access specific double value. scores是双精度数组,您需要在此数组中指定索引以访问特定的double值。
For example minValue = ptr[startScan].scores[0]; 例如minValue = ptr[startScan].scores[0];

minValue = ptr[startScan].scores; 

scores is an array of doubles. scores是一系列双打。 arrayname acts as a pointer as well. arrayname也是一个指针。 scores is of type double* [precisely one that can point to an array of size NUM_SCORES ] You are assigning a double pointer to an int. scoresdouble*类型[恰好可以指向大小为NUM_SCORES的数组]您正在为int指定一个double指针。

You're trying to assign double* (an array of doubles is a double*) to a double. 你试图将double *(双精度数组是double *)分配给double。

You can try changing your array of doubles into a vector of doubles. 你可以试着改变你的double型数组一个vector双打。 Then you can sort them using std::sort or std::stable_sort . 然后你可以使用std::sortstd::stable_sort对它们进行std::sort

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM