Sort function does not work with function object created on stack?

后端 未结 5 1825
旧时难觅i
旧时难觅i 2021-01-26 17:06
#include
#include
#include
class Integer
    {
public:
    int m;
    Integer(int a):m(a){};
    };
class CompareParts
            


        
5条回答
  •  无人及你
    2021-01-26 17:12

    #include
    #include
    #include
    

    class Integer
    {
    public:
    int m;
    Integer(int a):m(a){};
    };

    class CompareParts {
    public:
    bool operator()(const Integer & p1,const Integer & p2)
    {
    return p1.m }
    };

    int main()
    {
    std::vector vecInteger;
    vecInteger.push_back(Integer(12));
    vecInteger.push_back(Integer(13));
    vecInteger.push_back(Integer(5));
    vecInteger.push_back(Integer(7));
    vecInteger.push_back(Integer(9));

    std::sort(vecInteger.begin(),vecInteger.end(),CompareParts()); 
    typedef vector::const_iterator Iter;
    Iter beg = vecInteger.begin();
    Iter end = vecInteger.end();
    
    for (Iter iter = beg; iter != end; ++iter)
        cout << (*iter).m << " ";
    
    cout << endl;
    

    }

    Output: 5 7 9 12 13

提交回复
热议问题