简体   繁体   English

对对象数组的选择排序

[英]Selection Sort on an Array of Objects

Looked around online, but haven't been able to find anything concerning this. 在网上四处张望,但至今找不到任何相关信息。 I'm in a C++ introductory class and have a project that is dealing with Arrays of Objects. 我在C ++入门课程中,有一个正在处理对象数组的项目。 An object (called media) has a name and a rating. 对象(称为媒体)具有名称和等级。 I have three different media objects put into an media object array. 我将三个不同的媒体对象放入媒体对象数组。 I need to find a way to sort them by name. 我需要找到一种按名称对它们进行排序的方法。 I was given code for selection sort for c-strings, but haven't been able to figure out what exactly to do with this to change it over to dealing with the media objects. 为我提供了用于c字符串选择排序的代码,但还无法弄清楚将其转换为处理媒体对象的确切方法。 Here is my selection sort for the c-strings. 这是我对C弦的选择排序。

void selectsort(char str[][20], int N)
{
int pass, j, min;
char temp[20];
for (pass = 0; pass <= N - 2; pass++)  // passes
{
min = pass;
for (j = pass + 1; j < N; j++)  // in each pass
if (strcmp(str[min], str[j]) > 0)
min = j;
    strcpy(temp, str[min]);
    strcpy(str[min], str[pass]);
    strcpy(str[pass], temp);
    }
}

I have gotten rid of strcmp by overloading < with 我已经通过重载<和来摆脱了strcmp

int media::operator<(media med)
    {
        return strcmp(mname, med.mname);
    }

and strcpy by overloading = with 和strcpy通过重载=与

void media::operator=(media med)
    {
        strcpy(mname, med.mname);
    }

I believe that this would return the same results, yes? 我相信这将返回相同的结果,是吗?

So here is my code so far (have just been throwing myself at it non-understanding of how to do it). 到目前为止,这是我的代码(只是让我自己不了解如何做)。

void selsort(media mry[], int n) // mry[] is the object array, n is the 
                                 // number of objects in the array
{
    int pass, j, min;
    media temp[1];
    for (pass = 0; pass <= n - 2; pass++)  // passes
    {
        min = pass;
        for (j = pass + 1; j < n; j++)  // in each pass
            if ((mry[min] < mry[j]) < 0)
                min = j;
        temp = mry[min];
        mry[min] = mry[pass];
        temp = mry[min];
    }
}

I understand that there are a lot of flaws in this code. 我知道这段代码有很多缺陷。 The temp comparison doesn't work and when I try to get it to using *temp or temp[0] (trying everything under the sun over here) I get corrupted data errors. 临时比较无法正常工作,当我尝试使用* temp或temp [0](在此处尝试太阳下的所有操作)时,我得到了损坏的数据错误。 I can't seem to figure it out though. 我似乎无法弄清楚。 I've always struggled with understanding sorts, and this just adds insult to injury. 我一直在努力理解各种事物,这只会增加对侮辱的侮辱。 (Also please note that some notations might not be correct, but this is what I was taught in this class and what this professor asks that we code in) (还请注意,有些表示法可能不正确,但这是我在本课中所教的内容以及这位教授要求我们编写的内容)

Thanks for any help! 谢谢你的帮助!

There are 2 things wrong with your code: 您的代码有两件事:

  1. media temp[1]; Why would you want your temporary media storage for swapping be an array of 1? 您为什么要将用于交换的临时媒体存储区设置为1的数组?
  2. The swapping: temp = mry[min]; mry[min] = mry[pass]; temp = mry[min]; 交换: temp = mry[min]; mry[min] = mry[pass]; temp = mry[min]; temp = mry[min]; mry[min] = mry[pass]; temp = mry[min]; See anything wrong with it? 看到有什么问题吗?

Here is the adjusted function, which BTW works fine: 这是调整后的功能,BTW可以正常工作:

void selsort(media mry[], int n) // mry[] is the object array, n is the 
                                 // number of objects in the array
{
    int pass, j, min;
    media temp;
    for (pass = 0; pass <= n - 2; pass++)  // passes
    {
        min = pass;
        for (j = pass + 1; j < n; j++)  // in each pass
            if ((mry[min] < mry[j]) < 0)
                min = j;
        temp = mry[min];
        mry[min] = mry[pass];
        mry[pass] = temp;
    }
}

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

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