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

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

本文关键字:数组 排序 C++ 结构 指针 努力      更新时间:2023-10-16

所以我试图使用指针数组对结构数组进行排序。我正在尝试根据Test结构的int score成员对结构进行排序。我收到一堆错误,我认为它们都与我做错的具体事情有关。(或者我只是显然对这一切是如何工作的没有丝毫概念上的了解。

以下是错误:错误列表的屏幕截图

这是代码:

#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;
};
显然,我

并没有要求任何人为我做我的工作。但是有人可以指出我正确的方向吗?概念?也许指出我首先出错的地方并导致一连串的错误?

任何帮助非常感谢。

编辑:我搞砸了,忘了提到我正在工作的约束。1. 必须使用选择排序。

按对象的成员对对象的容器进行排序比这容易得多。

#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;
       }
    );
}
只有

1个整数的结构体这一事实并不能使它成为一个整数,它仍然是一个结构体。 为了比较对象(实际上是结构体产生的),你需要重载比较运算符,在你的例子中>运算符。