繁体   English   中英

对 class 数组进行升序排序

[英]Sorting class array in ascending order

对编程很陌生,需要一些帮助,我希望根据年龄按升序对 class 数组进行排序,但我只能让代码按降序工作。 我可能会忽略一些小事情,因为我已经在这方面工作了太久,所以我很感激任何帮助!

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

class Person 
{
    public:
        string name;
        int age;
    
    
        Person(string name = "empty", int age = 0) 
        {
            setName(name);
            setAge(age);
        }
        void setName(string x) {
            name = x;
        }
        
        string getName() {
            return name;
        }
        void setAge(int y) {
            age = y;
        }
        int getAge() {
            return age;
        }
        void displayinfo()
        {
            cout << "Name: " << name; cout << " Age: " << age << endl;
        }
};
void swap(Person &p, Person &q)
{
 Person temp;
 temp.name = p.name;
 temp.age = p.age;
 p.name = q.name;
 p.age = q.age;
 q.name = temp.name;
 q.age = temp.age;
}

int main() 
{
    
    int userValue;
    Person po("Jessica", 24);
    Person po2("Robert", 49);
    Person po3("Maria", 47);
    Person po4("John", 19);
    Person family[4] = {po,po2,po3,po4}; 
    
    int sort(family[4].getAge()); 
    {
        for(int i = 0; i < 3; i++)
            if (family[i].getAge() < family[i+1].getAge()) 
            {
                swap(family[i], family[i+1]);
            }
        
    }
    for(int i = 0; i < 4; i++)
        family[i].displayinfo();

}

int sort(family[4].getAge()); 不是 function 声明(并且您不能在另一个函数内部实现 function)。 它实际上是一个变量声明。 它声明了一个变量int sort ,该变量使用来自family[4].getAge()的值进行初始化(这是未定义的行为,因为索引 4超出了您的family[]数组的范围)。

因此,您要声明一个未使用的sort变量,然后输入您的for(int i = 0; i < 3; i++)循环,它不会执行数组的完整排序。 对于您正在尝试的内容,请改用标准std::sort()算法,例如:

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

class Person 
{
    public:
        string name;
        int age;
    
        Person(string name = "empty", int age = 0) 
        {
            setName(name);
            setAge(age);
        }

        void setName(string x) {
            name = x;
        }
        
        string getName() const {
            return name;
        }

        void setAge(int y) {
            age = y;
        }

        int getAge() const {
            return age;
        }

        void displayinfo() const
        {
            cout << "Name: " << name; cout << " Age: " << age << endl;
        }
};

int main() 
{
    Person po1("Jessica", 24);
    Person po2("Robert", 49);
    Person po3("Maria", 47);
    Person po4("John", 19);

    Person family[4] = {po1, po2, po3, po4};
    
    std::sort(family, family + 4,
        [](const Person &p1, const Person &p2) {
            return p1.getAge() < p2.getAge(); 
        }
    );

    for(int i = 0; i < 4; i++) {
        family[i].displayinfo();
    }

    return 0;
}

现场演示

请注意,您的family[]数组包含您初始化它的Person对象的副本 为了避免这些副本的开销,您可以对指针进行排序,例如:

int main() 
{
    Person po1("Jessica", 24);
    Person po2("Robert", 49);
    Person po3("Maria", 47);
    Person po4("John", 19);

    Person* family[4] = {&po1, &po2, &po3, &po4};
    
    std::sort(family, family + 4,
        [](const Person *p1, const Person *p2) {
            return p1->getAge() < p2->getAge(); 
        }
    );

    for(int i = 0; i < 4; i++) {
        family[i]->displayinfo();
    }

    return 0;
}

现场演示

或者,您可以完全摆脱单个p0...对象并直接初始化数组,例如:

int main() 
{
    Person family[4]{
        {"Jessica", 24},
        {"Robert", 49},
        {"Maria", 47},
        {"John", 19}
    };
    
    std::sort(family, family + 4,
        [](const Person &p1, const Person &p2) {
            return p1.getAge() < p2.getAge(); 
        }
    );

    for(int i = 0; i < 4; i++) {
        family[i].displayinfo();
    }

    return 0;
}

现场演示

暂无
暂无

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

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