按类成员的顺序对包含类对象的C++向量进行排序

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

本文关键字:向量 C++ 对象 排序 包含类 成员 顺序      更新时间:2023-10-16

我的目标是按照以下条件对向量进行排序; 我按"名称"对整个向量进行排序,当"名称"相等时,我希望它按"姓氏"等排序。下面是以下代码:

下面我创建了一个类;

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"的类成员对由类对象组成的向量进行排序:

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

我没有给出课堂上的所有代码,只是为了不分散你的注意力。但是,我离开了GitHub 如果您想在此处查看所有代码。

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

我深深地感谢你,如果你^^来到这条线上,请抽出时间。

有好的一天。

编辑:我忘了提到,当我调用sortVectorByDetail((函数时,我收到一个错误,说调试断言失败无效的比较器。 我在下面也保留了我以前调用这些函数的方式;

#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功能的问题。代码在这里;

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

为排序函数实现一个函数指针。

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中使用的所有功能。函数断言中的某个地方一定失败了。看看这个以获取更多信息

如果你想按字典顺序比较同一类的两个实例的某些"属性",你可以使用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());
}