#include <iostream>
#include <map>
using namespace std;
class Student {
public:
char name;
int score;
Student(char x, int y) :name(x), score(y)
{
}
bool operator <(const Student& s) const {
return this->score < s.score;
}
};
struct cmp {
bool operator()(Student s1, Student s2) const
{
return s1.score > s2.score;
}
};
void show(map<Student,int,cmp> cla)
{
for (map<Student, int>::iterator it = cla.begin();it!=cla.end();it++)
cout<<it->first.name<<" "<<it->first.score<<" "<<it->second<<endl;
cout<<endl;
}
int main()
{
map<Student, int, cmp> cla;
for (int i = 1; i <= 5; i++)
cla.insert(pair<Student,int>(Student(char('a'-1+i),i*10),i*10000));
show(cla);
cla.erase(cla.find(Student('b',20)));
show(cla);
cla[Student('f',60)]=60000,cla[Student('g',70)]=70000;
cout<<cla.count(Student('e',50))<<endl<<endl;
cla.erase(cla.find(Student('e',50)));
show(cla);
cout<<cla.count(Student('e',50))<<endl;
cla.clear();
system("pause");
}
STL中map映射函数代码
最新推荐文章于 2024-05-20 15:29:54 发布