【问题标题】:cannot sort an array of objects according to objects' attribute value无法根据对象的属性值对对象数组进行排序
【发布时间】:2021-07-13 11:21:22
【问题描述】:

Record 类包含月、日和余额。 创建记录对象数组后,我想按余额升序对其进行排序并显示它们。
然后,我想按日期升序排序并再次显示。
我能够按余额排序,但按日期排序时无法获得正确的顺序。
我按日期排序的方法是:

  1. 定义一个 isGreater 函数来比较对象 r1r2 的月份值,如果月份 r1
  2. sortByDate()中,实现冒泡排序和条件isGreater(arr[j],arr[j+1])
  3. 如果条件为真,则交换
    预期的输出应该是:
sorting according to balance:
9 14 -104.6
12 14 -104.5
12 13 -104.5

sorting according to date:
9 14 -104.6
12 13 -104.5
12 14 -104.5 

这是我的代码


#include <iostream>
using namespace std;

class Record {
private:
    int month, day;
    double balance;

public:
    void set(int m, int d, double b) { month = m; day = d; balance = b; }
    int getMonth() { return month; }
    int getDay() { return day; }
    double getBalance() { return balance; }
};




bool isGreater(Record r1, Record r2)
{
    if (r1.getMonth() > r2.getMonth())
    {
        return true;
    }
    else if (r1.getMonth() < r2.getMonth())
    {
        return false;
    }
    else
    {
        if (r1.getDay() > r2.getDay())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}


void sortByBalance(Record arr[], int n)
{
    int i, j;
    Record temp;
    for (i = 0; i < n - 1; i++)
    {
        for (j = 0; j < n - i - 1; j++)
        {
            if (arr[j].getBalance() > arr[j + 1].getBalance())
            {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

void sortByDate(Record arr[], int n)
{
    int i, j;
    Record temp;
    for (i = 0; i < n - 1; i++)
    {
        for (j = 0; j < n - i - 1; j++)
        {
            if (isGreater(arr[j], arr[j+1])); //{4.20, 3.15, 5.21, 6.21, 6.20}
            {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

void printArr(Record arr[], int n)
{
    for (int j = 0; j < n; j++)
    {
        cout << arr[j].getMonth() << ' ' << arr[j].getDay() << ' ' << arr[j].getBalance() << endl;
    }
}

int main()
{
    Record arr[30];
    int n;
    cout << "Enter the number of the records:" << endl;
    cin >> n;

    cout << "Enter the contents of each records:" << endl;
    int m, d;
    double b;
    for (int i = 0; i < n; i++)
    {
        cin >> m >> d >> b;
        arr[i].set(m, d, b);
    }

    cout << "Sort according to the balance:" << endl;
    sortByBalance(arr, n);
    printArr(arr, n);

    cout << "Sort according to the time:" << endl;
    sortByDate(arr, n);
    printArr(arr, n);   

}

【问题讨论】:

    标签: c++ arrays class sorting object


    【解决方案1】:

    如果您在打开编译器警告并注意它们的情况下构建它,您会注意到行尾的杂散分号

     if (isGreater(arr[j], arr[j+1])); 
    

    这有效地禁用了该检查并无论如何都执行交换。

    (也就是说,您应该使用 std::sort 而不是编写自己的冒泡排序。)

    【讨论】:

    • 谢谢!它现在按正确的顺序按日期排序。我根本没有注意到那个分号!从现在开始我会注意警告:D
    猜你喜欢
    • 1970-01-01
    • 2016-02-25
    • 2015-06-17
    • 2019-02-11
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 2017-07-18
    相关资源
    最近更新 更多