Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 正在努力使用指针数组对结构数组进行排序。C++;_C++_Arrays_Pointers_Struct - Fatal编程技术网

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

C++ 正在努力使用指针数组对结构数组进行排序。C++;,c++,arrays,pointers,struct,C++,Arrays,Pointers,Struct,因此,我尝试使用指针数组对结构数组进行排序。我试图根据测试结构的int score成员对结构进行排序。我犯了一大堆错误,我认为它们都与我做错的某个特定的事情有关。(或者我只是对这一切是如何运作的,显然没有一点概念上的把握。) 以下是错误: 代码如下: #include "stdafx.h" #include <iostream> #include <string> using namespace std; struct Test; void sele

因此,我尝试使用指针数组对结构数组进行排序。我试图根据
测试
结构的
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;
};
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
结构测试;
void selectionSort(int num,结构测试*sortArray[]);
int main()
{
const int NO_errors=0;
int-num,scoreIn;
字符串nameIn;

cout按对象的成员对对象容器进行排序要容易得多

#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;
       }
    );
}
#包括
#包括
#包括
结构测试
{
测试()
:分数(0)
,num(0)
{}
std::字符串名;
智力得分;
int-num;
};
int main()
{
const unsigned int num=5;//案例中的用户输入
std::向量v(num);
//!!为'v'中的项目赋值`
排序(
v、 begin(),
v、 end(),
[](恒速测试和lhs、恒速测试和rhs){
返回lhs.num
事实上,只有一个整数的struct并不意味着它是一个整数,它仍然是一个struct。为了比较对象(实际上是一个struct生成的)您需要重载比较运算符,在您的例子中是运算符。

int num=num;
这是什么?您可以对这一个进行排序
Test*tests=new Test[num];
而不是使用
sortArray
。尝试将
selectionSort()
更改为
void selectionSort(int num,Test*tests)
然后用它处理排序逻辑。你的编译器和工作环境是什么?如果你有兼容c++11的编译器,那么@Lightness Races in Orbit的答案是处理容器中的对象并对它们进行排序的方法。@Jagannath:我使用
Sortaray
的想法是保护原始数组不受任何可能的访问s/changes。但你是对的。省略它可能会容易得多。我要冒险一试:OP正在做功课=>他的教授希望他实现选择排序。+1。这是很好的参考资料。@Félixcanturnet:如果OP有约束,那么他应该在问题中明确地陈述它们。@Félixcanturnet:是的h、 我搞砸了,忘了提到我需要使用选择排序。