仿函数
完善优化中。。。
#include <iostream>
#include <Windows.h>
#include <set>
#include <functional>
#include <algorithm>
using namespace std
;
class student
{
public:
student(int age
)
{
this->age
= age
;
}
bool operator < (const student
& right
)const
{
return this->age
< right
.age
;
}
int getAge()const { return this->age
; }
private:
int age
;
};
class FunStudent
{
public:
bool operator()(const student
& left
, const student
& right
)const
{
cout
<< "FunStudent函数" << endl
;
return left
.getAge() < right
.getAge();
}
public:
};
int main(void)
{
set
<student
, FunStudent
> s
;
student
hh(18);
student
xx(80);
FunStudent fun
;
int ret
= fun(hh
, xx
);
cout
<< "比较结果:" << ret
<< endl
;
s
.insert(hh
);
s
.insert(xx
);
for (set
<student
, FunStudent
>::iterator it
= s
.begin(); it
!= s
.end(); ++it
)
{
cout
<< it
->getAge();
cout
<< " ";
}
cout
<< endl
;
system("pause");
return 0;
}
完善优化中。。。
转载请注明原文地址:https://blackberry.8miu.com/read-14394.html