#include<iostream>
#include <algorithm>
using namespace std;
class Student{
public:
string m_name;
int m_id;
int m_age;
Student(string name,int id,int age){
m_name=name;
m_age=age;
m_id=id;
}
void show(){
cout<<"name:"<<m_name<<" id:"<<m_id<<" age:"<<m_age<<endl;
}
};
bool comparator(const Student &o1,const Student &o2) {
return o1.m_id < o2.m_id;
}
int main(){
Student student1=Student("A",1,23);
Student student2=Student("B",2,21);
Student student3=Student("C",3,22);
Student students[3]={student3,student2,student1};
sort(students,students+3,comparator);
for(auto i:students)
i.show();
}
c++ 利用sort()函数对复杂结构数组排序(比较器的实现)
最新推荐文章于 2023-05-09 00:15:00 发布