//对结构体排序
// multiset 的排序规则, 如果两个东西的在排序方式中谁排在前面都一样,那么认为他们相等
#include<iostream>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
struct Student
{
char name[20];
int id;
double gpa;//绩点
};
Student students[] = {
{"Jack",112,3.4},{"Mary",102,3.8},
{"Mary",117,3.9},{"Ala",333,3.5},
{"Zero",101,4.0},{"Cindy",102,4.8}
};
struct Rule { //按gpa从高到低排序
bool operator()(const Student & s1, const Student & s2){
if(s1.gpa != s2.gpa)
return s1.gpa > s2.gpa;
else
return (strcmp(s1.name, s2.name) < 0);
//分数相等就按姓名排序
}
};
int main()
{
multiset<Student,Rule> st;
for(int i = 0; i < 5; ++i)
st.insert(students[i]);
multiset<Student,Rule>::iterator p;//迭代器
for(p = st.begin(); p != st.end(); p++)
cout << p->gpa << " " << p->name
<< " " << p->id << endl;
cout << endl;
//来个假Mary
Student s = {"Mary", 1000, 3.9};
p = st.find(s);
if(p != st.end())
cout << p->gpa << " " << p->name
<< " " << p->id << endl;
return 0;
}