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

时间:2015-03-16 09:36:28

标签: c++ arrays pointers struct

所以我试图通过使用指针数组来对结构数组进行排序。我试图根据int score结构的Test成员对结构进行排序。我收到了一堆错误,我认为它们都与某些特定的错误有关。 (或者我只是显然没有对这一切如何运作有任何概念性的把握。)

以下是错误: Screenshot of List of Errors

以下是代码:

#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.必须使用选择排序。

2 个答案:

答案 0 :(得分:2)

通过这些对象的成员对对象容器进行排序比这更容易。

#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 :(得分:0)

只有1个整数的struct不会使它成为整数,它仍然是一个结构。为了比较对象(事实上,结构生成的是什么),你需要重载比较运算符,在你的情况下&gt;运营商。