c++ - 如何使用数组按引用对对象进行排序? (以一种不那么愚蠢/复杂的方式。)

标签 c++ arrays pointers object reference

我写了这个,还有它的废话。我已经写了一个星期的代码了,所以请保持礼貌。以下代码的编写完全是因为我想在开始学习更高级的功能之前熟悉语言本身。

我的问题很宽泛。

在函数“sort_entries()”中,我使用冒泡排序根据从对象本身提取的值来随机排列数组中的对象引用。

但是,我想知道是否有更简单的方法来做到这一点?

事实上,我也想知道是否有更简单的方法来跟踪对象而不是通过指针数组引用它们,因为坦率地说我不喜欢传递指针。

#include <iostream>
#include <stdio.h>
#include <new>
#include <cstring>
using namespace std;

using namespace std;

class Person {
    int person_num;
    int waffles_eaten;

public:

    Person(int p_num, int waffles)
    {
        person_num = p_num;
        waffles_eaten=waffles;

        if(person_num == p_num && waffles_eaten == waffles)
        {
            printf("Entry #%i created. Waffles eaten = %i\n",    person_num, waffles_eaten);
        }
    }
    int get_num()
    {
        return person_num;
    }

    int get_eaten()
    {
        return waffles_eaten;
    }

};

/* ****************************** */

int num_entries()
{
    cout<<"How many entries to create?: ";
    int count;
    cin>>count;
    return count;
}

void create_entry(Person **registry, int count)
{
    int eaten;
    for(int i =0; i<count; i++)
    {
        printf("Person #%i: Waffles eaten? \n", i);
        cin>>eaten;
        registry[i] = new Person(i, eaten);
    }

}

void view_entries(Person **registry, int count)
{
    for(int i=0; i<count; i++)
    {
        printf("Person #%i ate %i waffles\n", (registry[i])->get_num(), (registry[i])->get_eaten() );
    }

}

void delete_entries(Person **registry, int count)
{
    for(int i=0; i<count; i++)
    {
        delete [] registry[i];
    }
}

void cpy_reg(Person **registry, Person **sorted_reg, int count)
{
    for(int i=0; i<count; i++)
    {
        sorted_reg[i] = registry[i];
    }
}

void display_data(Person **sorted_reg, count int)
{

}


void sort_entries(Person **registry, Person **sorted_reg, int count) // Need to figure why this actually works
{
    cpy_reg(registry, sorted_reg, count); // copy the contents of registry[] to sorted_reg[]

    cout<<"Sorted List"<<endl;
    cout<<"------------"<<endl;

    /* does magical sorting stuff */

    int i, j;
    Person *temp;

    for(i=0; i<count; i++)
    {
        for(j=i+1; j<count; j++)cl
        {
            if( ((sorted_reg[i])->get_eaten() ) > ((sorted_reg[j])->get_eaten()) ) 
            {
                temp = *(sorted_reg + j);
                *(sorted_reg+j) = *(sorted_reg+i);
                *(sorted_reg+i) = temp;
            }
        }
    }
}

void print_values(Person **reg, int count)
{
    for(int i=0; i<count; i++)
    {
        printf("Entry #%i, --> %i waffles\n", i, (reg[i])->get_eaten() ); 
    }
}

bool ask_to_sort()
{
    string in;
    bool out;
    cout<<"Sort entries, and see data about them? (Y/N)"<<endl;
    cin>>in;
    if( in=="y" || in =="Y")
    {
        return true;
    }else if(in=="n"||in=="N")
    {
        cout<<"Exiting"<<endl;
    }
    else {
        cout<<"Enter Y/N"<<endl;
    }
}

/* **************************** */  

int main()
{
    int count = num_entries();
    Person *registry[count];
    Person *sorted_reg[count];
    create_entry(registry, count);
    view_entries(registry, count);

    if(ask_to_sort())
    {
        sort_entries( registry, sorted_reg, count);
    }

    print_values(sorted_reg, count);

    delete_entries(registry, count);

    return 0;
}

最佳答案

正如其他人所提到的,在 C++ 中创建可变长度数组是无效的。 有 std::vector在这些时候,你应该使用它。

所以不是这个:

Person *registry[count];

你应该这样做:

std::vector<Person> registry(count);

当您可以使用 Person 实例时,无需使用指针,这使代码更易于理解和维护。

因此在排序函数中交换两个项目,而不是这样:

temp = *(sorted_reg + j);
*(sorted_reg+j) = *(sorted_reg+i);
*(sorted_reg+i) = temp;

这样做:

swap(sorted_reg[i], sorted_reg[j])

只是交换值。

毕竟,要对 vector 进行排序,您可以依赖 STL's sort function .您需要为 operator<() 定义一个小于运算符 ( Person )结构。

关于c++ - 如何使用数组按引用对对象进行排序? (以一种不那么愚蠢/复杂的方式。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34347113/

相关文章:

c - 二维数组指针符号适用于一个版本但不适用于另一个版本......我认为

c++ - 如何在模板派生类中调用模板基类的构造函数?

c++ - 查找 vector 构造的交点

c++ - ifstream 在循环中不起作用

php - 从一组字符串中获取可用数组的复杂方法

c++ - 什么是可变常量?

c++ - 在opengl中移动相机时显示错误

java - java中如何给数组添加值

java - 如何在Java中存储和检查字符串的同义词

c - 驻留在一个内存地址的两个整数变量?