首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

STL中对自定义类型的排序有关问题

2013-09-09 
STL中对自定义类型的排序问题代码如下:#include list#include iostreamusing namespace stdclass tes

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;
}


结果是最后两个对象的排序不正确。希望高手们帮忙看看,谢谢了~@!!!
stl 类 对象 class it
[解决办法]
return (double(i) / t) > (double(rec.i) / rec.t );

热点排行