【问题标题】:Sorting class array in ascending order按升序对类数组进行排序
【发布时间】:2020-07-23 23:24:07
【问题描述】:

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

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

}

【问题讨论】:

    标签: c++


    【解决方案1】:

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

    因此,您声明了一个未使用的 sort 变量,然后您进入了您的 for(int i = 0; i &lt; 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

    【讨论】:

    • 而不是std::sort(family, family + 4, ...),最好使用std::sort(std::begin(family), std::end(family), ...)。然后,如果数组 family 中的元素数量发生变化,sort() 的使用将不会发生变化。类似地,显示数组元素的循环可以是基于范围的for,或者是从头到尾迭代的循环。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多