当前位置: 代码迷 >> 综合 >> multiset 的排序规则
  详细解决方案

multiset 的排序规则

热度:27   发布时间:2023-11-23 05:24:10.0
//对结构体排序
// 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;elsereturn (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;//来个假MaryStudent s = {"Mary", 1000, 3.9};p = st.find(s);if(p != st.end())cout << p->gpa << " " << p->name<< " " << p->id << endl;return 0;
}