1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include<iostream> #include<set> #include<string> #include<cstdio> using namespace std; struct Student { int id; string name; }stu1,stu2,stu3; struct setcmp//set容器的插件,来确定set内部的排序 { bool operator()(Student a,Student b) { return a.id>b.id; //这个大括号不可以省略,不然会报错的 } }; struct Setcmp { bool operator()(Student a,Student b) { return a.id<b.id; } }; int main() { set<Student,setcmp> my; set<Student>::iterator it; stu1.id=1000; stu2.id=2000; stu3.id=3000; stu1.name="ddd"; stu2.name="www"; stu3.name="aaa"; my.insert(stu1); my.insert(stu2); my.insert(stu3); for(it=my.begin();it!=my.end();it++) { cout<<it->id<<" "<<it->name<<endl; } set<Student,Setcmp> mm; mm.insert(stu1); mm.insert(stu2); mm.insert(stu3); for(it=mm.begin();it!=mm.end();it++) { cout<<it->id<<" "<<it->name<<endl; } return 0; }
|