简体   繁体   English

尝试对结构数组使用选择排序

[英]Trying to use selection sort on an array of structs

I've got an array of structs, and I'm trying to sort it by price using selection sort. 我有一系列结构,我正在尝试使用选择排序按价格对其进行排序。 I'm consistently getting a ECX_BAD_ACCESS signal. 我一直在收到一个ECX_BAD_ACCESS信号。

This is what I've got: 这就是我所拥有的:

#include <iostream>
#include <string>
using namespace std;

So thing is just a struct with a name and a price. 所以事情只是一个名称和价格的结构。 The price is what I want to sort the structs by 价格是我想要排序的结构

struct Thing
{
    std::string name;
    double price;
};

Right now, the main method just checks to make sure that the sortThing function is working. 现在,main方法只是检查以确保sortThing函数正常工作。

Thing *sortThing(Thing input[], int size);

int main ()
{
    Thing *stuff = new Thing[3];

    stuff[0].name = "middle";
    stuff[0].price = 2.00;
    stuff[1].name = "last";
    stuff[1].price = 3.00;
    stuff[2].name = "first";
    stuff[2].price = 1.00;

    stuff = sortThing(stuff, 3);

    cout << stuff[0].name + " " + stuff[1].name + " " + stuff[2].name;

    return 0;
}

This is where the problem is. 这就是问题所在。 It complains about the line where I assign input[maxIndex].name to output[i].name, saying: "Program Received 'EXC_BAD_ACCESS'" 它抱怨我将输入[maxIndex] .name分配给output [i] .name的行,说:“Program Received'EXC_BAD_ACCESS'”

Thing *sortThing(Thing input[], int size)
{
    Thing *output = new Thing[size];
    Thing nullThing;
    nullThing.name = "&nullThing";
    int min = 0;
    int minIndex;

    for (int i = 0; i < size; i++)
    {

        for (int j = 0; j < size; j++)
        {
            if (input[j].name.compare("&nullThing") != 0 && input[j].price <= min) minIndex = j;
        }
        output[i].name = input[minIndex].name;
        output[i].price = input[minIndex].price;
        input[minIndex] = nullThing;
        minIndex = 0;
        min = 0;
    }

    return output;
}

Any help is appreciated 任何帮助表示赞赏

I think your loop for determining minIndex is incorrect... Since all prices are >= 0, you will never set minIndex , and since you never initialized it, it's value can be anything. 我认为你确定minIndex的循环是不正确的...因为所有的价格都是> = 0,你永远不会设置minIndex ,因为你从未初始化它,它的值可以是任何东西。 Most likely in your case the value is outside the range of the array and effectively having an array out of bounds error. 在您的情况下,最有可能的是,该值超出了数组的范围,并且有效地使数组超出了界限误差。

Might want to look into some sorting algorithms. 可能想要研究一些排序算法。

Debugging 101: here's the first thing to do. 调试101:这是第一件事。 Change: 更改:

int minIndex;

to: 至:

int minIndex = -42;

(temporarily) then output the value of minIndex immediately after the inner loop. (暂时)然后在内循环后立即输出minIndex的值。

This will show you that the value is not being changed. 这将显示该值未被更改。

The reason it's not being changed is the initial setting of min . 它没有被改变的原因min的初始设置。 Unless you're giving your product away for free, or paying people to take it away, prices will always be greater than zero and the second part of the if statement will always be false. 除非你免费赠送你的产品,或付钱让人拿走它,否则价格总是大于零,而if语句的第二部分总是假的。

Quick fix, set min initially to INT_MAX (and also where you reset it at the end of the outer loop). 快速修复,最初将min设置为INT_MAX (以及在外部循环结束时重置它的位置)。

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

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