STL中对自定义类型的排序问题
代码如下:
#include <list>
#include <iostream>
using namespace std;
class test
{
public:
int i;
int t;
test(int j,int k)
{
i = j;
t = k;
}
bool operator >(const test& rec) const
{
return (int)(i/t) > (int)(rec.i/rec.t);
}
};
int main()
{
test a1(2,3);
test a2(3,2);
test a3(1,2);
test a4(3,4);
//test a4(5,2);
list<test> a;
a.push_back(a1);
a.push_back(a2);
a.push_back(a3);
a.push_back(a4);
a.sort(greater<test>());
list<test>::iterator it = a.begin();
for(it; it != a.end(); it++)
{
cout<<it->i<<" "<<it->t<<endl;
}
cout<<endl;
return 0;
}