按升序对类数组进行排序

时间:2020-07-23 23:24:07

标签: c++

对于编程非常陌生,需要一些帮助,我希望根据年龄对类数组进行升序排序,但是我只能使代码按降序工作。我从事这项工作太久了,所以我可能会忽略一些小事情,感谢您的帮助!

#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();

}

1 个答案:

答案 0 :(得分:0)

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

因此,您要声明一个未使用的sort变量,然后输入for(int i = 0; i < 3; i++)循环,该循环不执行数组的 full 排序。对于您要尝试的操作,请改用标准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;
}

Live Demo

请注意,您的family[]数组保存用来初始化的Person对象的副本。为了避免这些副本的开销,您可以对 pointers 进行排序,例如:

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;
}

Live Demo

或者,您可以完全摆脱单个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;
}

Live Demo