簡體   English   中英

按 class 成員的順序在 C++ 中對包含 class 對象的向量進行排序

[英]Sorting vectors, that contains class objects, in C++ by class member's in order

我的目標是我想按照以下標准對向量進行排序; 我按“name”對整個向量進行了排序,當“name”相等時,我希望它按“surname”等排序。這是以下代碼:

下面我創建了一個 class;

class NotLearning {
public:
    NotLearning(); // Constructor
    int getID()const; // Function to used to initialise private variable "ID" of the class via the constructor.
    int getDay()const; // Function to used to initialise private variable "day" of the class via the constructor.
    int getYear()const; // Function to used to initialise private variable "year" of the class via the constructor.

    double getAverage()const; // Function to used to initialise private variable "average" of the class via the constructor.

    std::string getName()const; // Function to used to initialise private variable "name" of the class via the constructor.
    std::string getSurname()const; // Function to used to initialise private variable "surname" of the class via the constructor.
    std::string getCity()const; // Function to used to initialise private variable "city" of the class via the constructor.
    std::string getMonth()const; // Function to used to initialise private variable "month" of the class via the constructor.

private:
    int ID;
    int day;
    int year;

    double average;

    std::string name;
    std::string surname;
    std::string city;
    std::string month;
};

我使用“ID”的 class 成員對包含 class 對象的向量進行排序,代碼如下:

void sortVectorByID(std::vector<NotLearning>& newClass)
{
    std::sort(newClass.begin(), newClass.end(), cmp_by_id);
}

其中“ cmp_by_id ”是這樣定義的;

bool cmp_by_id(const NotLearning& one, const NotLearning& two) // compare classes inside the vector by checking the member of "ID".
{
    return one.getID() < two.getID();
}

到目前為止,我已經成功地對向量進行了排序。 之后,我為我的目的實現了以下代碼;

void sortVectorByDetail(std::vector<NotLearning>& newClass)
{
    std::sort(newClass.begin(), newClass.end(), cmp_by_detail);
}

其中“ cmp_by_detail ”是這樣定義的;

bool cmp_by_detail(const NotLearning& one, const NotLearning& two) // compare classes inside the vector by checking the members in order.
{
    bool compare_result = cmp_by_id(one, two);
    if (compare_result)
        return compare_result;

    compare_result = cmp_by_average(one, two);
    if (compare_result)
        return compare_result;

    compare_result = cmp_by_name(one, two);
    if (compare_result)
        return compare_result;

    compare_result = cmp_by_surname(one, two);
    if (compare_result)
        return compare_result;

    compare_result = cmp_by_city(one, two);
    if (compare_result)
        return compare_result;

    compare_result = cmp_by_day(one, two);
    if (compare_result)
        return compare_result;

    compare_result = cmp_by_month(one, two);
    if (compare_result)
        return compare_result;

    compare_result = cmp_by_year(one, two);
    if (compare_result)
        return compare_result;

    return true;
}

我沒有給出 class 中的所有代碼,只是為了不分散你的注意力。 但是,如果您想查看所有代碼,我將GitHub留在這里。

我希望我能告訴你我需要什么。

如果您來到這條線,我非常感謝您節省您的時間^^。

祝你今天過得愉快。

編輯:我忘了提到當我調用sortVectorByDetail() function 時,我收到一個錯誤,提示調試斷言失敗無效比較器 我也在下面離開了我過去調用這些函數的方式;

#include "LearningCpp.h"


int main()
{
    srand((unsigned int)time(NULL));

    std::vector<NotLearning> myClass;
    fillVector(myClass);
    showVector(myClass);

    sortVectorByDetail(myClass);
    showVector(myClass);  
}

編輯:我現在通過對 cmp_by_detail 的function的以下實現解決了我的問題。 代碼在這里;

bool cmp_by_detail(const NotLearning& one, const NotLearning& two) {

    if (one.getID() != two.getID())
    {
        return one.getID() < two.getID();
    }

    if (one.getAverage() != two.getAverage())
    {
        return one.getAverage() < two.getAverage();
    }

    if (one.getName() != two.getName())
    {
        return one.getName() < two.getName();
    }

    if (one.getSurname() != two.getSurname())
    {
        return one.getSurname() < two.getSurname();
    }

    if (one.getCity() != two.getCity())
    {
        return one.getCity() < two.getCity();
    }

    if (one.getDay() != two.getDay())
    {
        return one.getDay() < two.getDay();
    }

    if (one.getMonth() != two.getMonth())
    {
        return one.getMonth() < two.getMonth();
    }

    if (one.getYear() != two.getYear())
    {
        return one.getYear() < two.getYear();
    }

    return true;
}

實現一個 function 指針用於排序 function。

bool cmp(NotLearing a, NotLearning b)
{
    if(a.getName()==b.getName())
       return a.getSurname()<b.getSurname();
    return a.getName<b.getName();
}

然后對你的向量進行排序

vector<NotLearning> v;
sort(v.begin(),v.end(),cmp); 

我認為您應該檢查您在 cmp_by_detail 中使用的所有cmp_by_detail function 中的某處斷言必須失敗。 看看這個以獲取更多信息

如果您想按字典順序比較相同 class 的兩個實例的某些“屬性”,您可以使用std::forward_as_tuple 這樣你的cmp_by_detail實現就變成了......

bool cmp_by_detail (const NotLearning &one, const NotLearning &two)
{
    return std::forward_as_tuple(one.getID(), one.getAverage(), one.getName(),
                                 one.getSurname(), one.getCity(), one.getDay(),
                                 one.getMonth(), one.getYear())
         < std::forward_as_tuple(two.getID(), two.getAverage(), two.getName(),
                                 two.getSurname(), two.getCity(), two.getDay(),
                                 two.getMonth(), two.getYear());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM