依然是用到前面所提的student类来做演示
#include<iostream>
#include<set>
#include"student.h"
using namespace std;
bool stucmp(const student&s1,const student& s2) //同样是因为set是要求录入的是可以排序比较的,所以我们定义一个函数指针
{
return s1.getId() > s2.getId();
}
typedef bool(*fun_cmp)(const student&, const student&);
int main()
{
set<student, fun_cmp> stus(stucmp);
student stu1("Sechs", 100);
student stu2("SechsEN", 99);
stus.insert(stu1);
stus.insert(stu2);
set<student>::iterator it;
for (it = stus.begin(); it != stus.end(); it++)
{
it->showname();
}
set<student>::iterator fit = stus.find(stu1); //查找find()函数中的
if (fit==stus.end()) //find()如果没有找到的话就会给迭代器返回end()
{
cout <<"there is no such student"<< endl;
}
else
{
fit->showname();
}
}