我定义了一个链表,list<CMontew> lm1;
CMontew里面
{char s[5];
float para1;
float para2;
float randf1;
}
我想请教一下各位,怎么样对这个键表lm1按randf1的大小排序???
STL中好像有直接排序的命令??
请请各位指教!

解决方案 »

  1.   

    list本身有成员函数sort,对list'而言这个比模版函数效率高
      

  2.   

    class CMontew_less
    {
    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 函数对象
      

  3.   

    1楼的意思,不太懂,不好意思,我对C++和VC还不是很熟。您能更说得更透彻些么?谢谢,最好有个小例子
      

  4.   

    #include "StdAfx.h"#include <iostream>
    #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,   " "));
    }
      

  5.   

    纠正一下
    不是效率高而是根本不能用std::sort
    erase才是效率更高