multimap的用法
multimap容器里的元素,都是pair形式的
则mp里的元素都是如下类型的:
1 2 3 4
| struct { T1 first; T2 second; }
|
multimap中的元素按照first进行排序,并可以按first进行查找
**缺省的排序规则是a.first < b.first
**时为true
,则a排在b前面
multimap的应用
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 52 53
| #include<iostream> #include<map> #include<cstring> using namespace std; struct StudentInfo { int id; char name[20]; }; struct Student { int score; StudentInfo info; }; typedef multimap<int, StudentInfo> MAP_STD; int main() { MAP_STD mp; Student st; char cmd[20]; while( cin >> cmd ) { if( cmd[0] == 'A' ) { cin>>st.info.name >> st.info.id>> st.score; mp.insert(make_pair(st.score, st.info)); } else if( cmd[0] == 'Q'){ int score; cin>>score; MAP_STD::iterator p = mp.lower_bound (score); if( p != mp.begin()) { --p; score = p->first; MAP_STD::iterator maxp = p; int maxId = p->second.id; while(p!= mp.begin() && p->first == score){ if(p->second.id > maxId ){ maxp = p; maxId = p->second.id; } p--; } if(p->first == score){ if(p->second.id > maxId ){ maxp = p; maxId = p->second.id; } } cout<< maxp->second.name <<' '<< maxp->second.id <<' '<< maxp->first <<endl; } else cout<< "Nobody" << endl; } } return 0; }
|