簡體   English   中英

努力使用指針數組對結構數組進行排序。 C ++

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

所以我試圖通過使用指針數組來對結構數組進行排序。 我正在嘗試根據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;
       }
    );
}

實際上只有一個整數的struct並不能使它成為整數,它仍然是一個struct。 為了比較對象(實際上是一個結構產生的對象),您需要重載比較運算符,在這種情況下,請重載>運算符。

暫無
暫無

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

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