/*
multimap容器里面的元素,都是pair形式的
multimap<T1, T2> mp;
则 mp 里面都是这样的:
struct{
T1 first; //关键字
T2 second; //值
};
multimap 中的元素按照first排序,并可以按first进行查找
缺省的排序规律的 “a.first < b.first” 为true,则a排在b前面;
应用:
一个学生成绩录入查询系统
Add name id score
Query score
name是一个不超过16的字符串,中间没有空格,代表学生姓名。id是个整数,
代表学号。score是成绩。学号不会重复,分数和姓名都可能重复。
两种输入交替出现。第一种表示添加学生信息。第二种是查询,碰到这种输入,
就输出就中分数比score第的最高分获得者的信命,学号,分数。,多个学生分数相同,
输出学号大的那个学生的信息。找不到输出nobody;
make_pair生成一个pair<int, Studentinfo>变量
其first等于 st.score, second 等于 st.info
*/
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
struct StudentIofo{
int id;
char name[20];
};
struct Student{
int score;
StudentIofo info;
};
typedef multimap<int,StudentIofo> MAP_STD;
//此后 MAP_STD 等价于 multimap<int,StudentIofo>
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));
//make_pair生成一个pair<int, Studentinfo>变量
//其first等于 st.score, second 等于 st.info
}
else if( cmd[0] == 'Q')
{
int score;
cin >> score;
//寻找比score低的最高分
MAP_STD::iterator p = mp.lower_bound(score);
if( p != mp.begin())
{
--p;
score = p->first;
//还有找其中id最高的学生
MAP_STD::iterator maxp = p;
int maxId = p->second.id;
for(; p != mp.begin() && p->first == score; --p)
{
//遍历所有成绩和score相等的学生
if(p->second.id > maxId)
{
maxp = p;
maxId = p->second.id;
}
}
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;
}
//lower_bound的结果就是begin,说明没人分数比查询分数低
else cout << "Nobody" << endl;
}
}
return 0;
}
/*
Add Jack 12 78
Query 78
Nobody
Query 81
Jack 12 78
Add Percy 9 81
Add Mary 8 81
Query 82
Percy 9 81
Add Tom 11 79
Query 80
Tom 11 79
Query 81
Tom 11 79
*/