简体   繁体   English

努力使用指针数组对结构数组进行排序。 C ++

[英]Struggling to sort an array of structs using an array of pointers. C++

So I'm trying to sort an array of structs by using an array of pointers. 所以我试图通过使用指针数组来对结构数组进行排序。 I'm trying to sort the struct based on the int score member of the Test struct. 我正在尝试根据Test结构的int score成员对结构进行排序。 I'm getting a bunch of errors and I think they are all related to something specific that I am doing wrong. 我遇到了很多错误,我认为它们都与我做错的特定事情有关。 (Or I just clearly don't have the slightest conceptual grasp about how this all works.) (或者我只是显然对这一切的工作原理没有丝毫的了解。)

Here are the errors: Screenshot of List of Errors 错误如下: 错误列表的屏幕截图

And here is the code: 这是代码:

#include "stdafx.h"  
#include <iostream> 
#include <string>   


using namespace std; 

struct Test;
void selectionSort(int num, struct Test* sortArray[]);


int main()
{
    const int NO_ERRRORS = 0;

    int num, scoreIn;
    string nameIn;


    cout << "Please provide the number of test scores you would " << endl
        << "like to average and sort." << endl << endl
        << "Please limit your request to 5-20 tests: ";
    cin >> num;
    while ((num < 5) || (num > 20))
    {
        cout << "Invalid entry. Please enter an integer between 5-20: ";
        cin >> num;
    }
    cout << endl;

    Test* tests = new Test[num];

    Test** sortArray = new Test*[num];

    cout << "Please enter first names only with no spaces." << endl << endl;
    for (int index = 0; index < num; index++)
    {
        cout << "Please enter the name of student " << (index + 1) << ": ";
        cin >> nameIn;
        cout << "Enter the test score: ";
        cin >> scoreIn;
        while (scoreIn < 0)
        {
            cout << "Invalid entry. Please enter a positive integer: ";
            cin >> scoreIn;
        }
        cout << endl;

        ((*(tests + index)) = { nameIn, scoreIn }); //IntelliSense: no operator "=" matches operands. Operand types are Test = {..}  
        sortArray[index] = &tests[index];
    }

    selectionSort(num, sortArray);

    for (int count = 0; count < num; count++)
         cout << (sortArray[count]->score) << " ";
    cout << endl;


    for (int count = 0; count < num; count++)
        cout << (sortArray[count]->name) << " ";
    cout << endl;


    delete[] tests;


    cin.ignore(cin.rdbuf()->in_avail(), '\n');
    cout << endl << "Press only the 'Enter' key to exit program: ";
    cin.get();

    return NO_ERRRORS;

}

void selectionSort(int num, struct Test* sortArray[])
{


    int minIndex;
    Test *minElem;
    for (int scan = 0; scan < (num - 1); scan++)
    {
        minIndex = scan;
        minElem = sortArray[scan];
        for (int index = scan + 1; index < num; index++)
        {
            if ((*(sortArray[index])) < (*minElem)) //IntelliSense: no operator "<" matches operands. Operand types are Test < Test 
            {
                minElem = sortArray[index];
                minIndex = index;
            }
        }
        sortArray[minIndex] = sortArray[scan];
        sortArray[scan] = minElem;
    }
}

struct Test
{
    int num = num;

     Test()
    {
            name = "";
            score = 0;
    }
    string name;
    int score;
};

Obviously I'm not asking anyone to do my work for me. 显然,我并没有要求任何人为我做我的工作。 But could someone maybe point me in the right direction...conceptually? 但是有人可以……从概念上向我指出正确的方向吗? Maybe point out where I go wrong first and cause the cascade of errors? 也许指出我首先出错的地方并导致了一系列错误?

Any help very much appreciated. 任何帮助,非常感谢。

Edit: I messed up and forgot to mention the constraints I am working in. 1. Must use a selection sort. 编辑:我搞砸了,忘了提到我正在工作的约束。1.必须使用选择排序。

Sorting a container of objects by members of those objects is much easier than this. 通过这些对象的成员对对象容器进行排序比这容易得多。

#include <vector>
#include <algorithm>
#include <string>

struct Test
{
    Test()
       : score(0)
       , num(0)
    {}

    std::string name;
    int score;
    int num;
};

int main()
{
    const unsigned int num = 5;  // user input in your case
    std::vector<Test> v(num);

    //!! Assign values to items in `v`

    std::sort(
       v.begin(),
       v.end(),
       [](const Test& lhs, const Test& rhs) {
          return lhs.num < rhs.num;
       }
    );
}

the fact that there is struct with just 1 integer does not makes it an integer , it's still a struct. 实际上只有一个整数的struct并不能使它成为整数,它仍然是一个struct。 in order to compare objects (which in fact , what a struct produces) you need to overload the comparison operator , and in your case > operator. 为了比较对象(实际上是一个结构产生的对象),您需要重载比较运算符,在这种情况下,请重载>运算符。

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

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