一、用法
二、应用
#include<iostream>
#include<map>
using namespace std;
struct studentinfo
{
int id;
char name[20];
};
struct student //multimap容器内元素是pair形式
{
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)); //使得输入以pair形式存入
}
else if (cmd[0] == 'Q')
{
int score;
cin >> score;
auto p = mp.lower_bound(score);
if (p != mp.begin())
{
--p;
score = p->first;
MAP_STD::iterator maxp = p;
int maxid = p->second.id;
for (; p != mp.begin() && p->first == score; --p)
{
if (p->second.id > maxid)
{
maxp = p;
maxid = p->second.id;
}
}
if (p->first == score) //即是因为指向begin()跳出,此时仍需比较
{
if (p->second.id > maxid)
{
maxp = p;
maxid = p->second.id;
}
}
cout << maxp->second.name << " " << maxp->second.id << " " << maxp->first << endl;
}
else //即p==mp.begin() 没有比其更低的了
cout << "Nobody" << endl;
}
}
system("pause");
}