我定义了一个链表,list<CMontew> lm1;
CMontew里面
{char s[5];
float para1;
float para2;
float randf1;
}
我想请教一下各位,怎么样对这个键表lm1按randf1的大小排序???
STL中好像有直接排序的命令??
请请各位指教!
CMontew里面
{char s[5];
float para1;
float para2;
float randf1;
}
我想请教一下各位,怎么样对这个键表lm1按randf1的大小排序???
STL中好像有直接排序的命令??
请请各位指教!
{
public:
CMontew_less() {}
bool operator () (const CMontew* m1, const CMontew* m2)
{
return (m1->randf1 < m2->randf1);
}
}使用std::sort(lm1.begin(), lm1.end(), CMontew_less);排序参考:google 函数对象
#include <list>
#include <algorithm>using namespace std;struct CMontew
{
char s[5];
float para1;
float para2;
float randf1; CMontew(float f_a, float f_b, float f_c)
:para1(f_a), para2(f_b), randf1(f_c){} friend bool operator < (const CMontew& ref_l, const CMontew& ref_r)
{
return ref_l.randf1 < ref_r.randf1;
} friend ostream& operator << (ostream& o_out, const CMontew& ref_r)
{
return o_out << "("
<< ref_r .para1 << " "
<< ref_r .para2 << " "
<< ref_r .randf1 << " "
<< ")";
}
};void main()
{
list <CMontew> listM; listM.push_back(CMontew(1, 2, 2.5));
listM.push_back(CMontew(2, 2, 2.8));
listM.push_back(CMontew(3, 2, 1.3)); copy(listM.begin(), listM.end(), ostream_iterator <CMontew> (cout, " ")); listM.sort(); cout << endl; copy(listM.begin(), listM.end(), ostream_iterator <CMontew> (cout, " "));
}
不是效率高而是根本不能用std::sort
erase才是效率更高