按类成员的顺序对包含类对象的C++向量进行排序
Sorting vectors, that contains class objects, in C++ by class member's in order
我的目标是按照以下条件对向量进行排序; 我按"名称"对整个向量进行排序,当"名称"相等时,我希望它按"姓氏"等排序。下面是以下代码:
下面我创建了一个类;
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());
}
相关文章:
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- 变量没有改变?通过向量的函数调用
- 迭代时从向量和内存中删除对象
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 计算排序向量的向量中唯一值的计数
- 矩阵向量乘法(cublasDgemv)返回零
- 一对向量构造函数:初始值设定项列表与显式构造
- 将结构向量排序为子组
- 在C++中调整向量中的索引
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么