How to implement sorting method for a c++ priority_queue with pointers

前端 未结 4 1624
耶瑟儿~
耶瑟儿~ 2021-01-12 00:23

My priority queue declared as:

std::priority_queue<*MyClass> queue;

class MyClass {
    bool operator<( const MyClass* m ) const;
}
相关标签:
4条回答
  • 2021-01-12 00:56

    The operator <() you have provided will compare a MyClass object with a pointer to a MyClass object. But your queue contains only pointers (I think). You need a comparison function that takes two pointers as parameters.

    All this is based on some suppositions - please post your actual code, using copy and paste.

    0 讨论(0)
  • 2021-01-12 00:57

    Not sure about the priority queue stuff because I've never used it but to do a straight sort, you can do this:

    class A
    {
        friend struct ComparePtrToA;
    public:
        A( int v=0 ):a(v){}
    private:
        int a;
    };
    
    struct ComparePtrToA
    {
        bool operator()(A* a1, A* a2) {return a1->a < a2->a;}
    };
    
    #include <vector>
    #include <algorithm>
    int _tmain(int argc, _TCHAR* argv[])
    {
        vector<A*> someAs;
        someAs.push_back(new A(1));
        someAs.push_back(new A(3));
        someAs.push_back(new A(2));
        sort( someAs.begin(), someAs.end(), ComparePtrToA() );
    }
    

    Note the memory leaks, this is only an example...

    Further note: This is not intended to be an implementation of priority queue! The vector is simply an example of using the functor I created to compare two objects via their pointers. Although I'm aware of what a priority queue is and roughly how it works, I have never used the STL features that implement them.

    Update: I think TimW makes some valid points. I don't know why he was downvoted so much. I think my answer can be improved as follows:

    class A
    {
    public:
        A( int v=0 ):a(v){}
        bool operator<( const A& rhs ) { return a < rhs.a; }
    private:
        int a;
    };
    
    struct ComparePtrToA
    {
        bool operator()(A* a1, A* a2) {return *a1 < *a2;}
    };
    

    which is cleaner (especially if you consider having a container of values rather than pointers - no further work would be necessary).

    0 讨论(0)
  • 2021-01-12 00:59

    Since your priority_queue contains only pointer values, it will use the default comparison operator for the pointers - this will sort them by address which is obviously not what you want. If you change the priority_queue to store the class instances by value, it will use the operator you defined. Or, you will have to provide a comparison function.

    0 讨论(0)
  • 2021-01-12 01:00

    Give the que the Compare functor ptr_less.

    If you want the ptr_less to be compatible with the rest of the std library (binders, composers, ... ):

    template<class T>
    struct ptr_less
        : public binary_function<T, T, bool> {  
            bool operator()(const T& left, const T& right) const{
                return ((*left) <( *right));
            }
    };
    
    std::priority_queue<MyClass*, vector<MyClass*>, ptr_less<MyClass*> > que; 
    

    Otherwise you can get away with the simplified version:

    struct ptr_less {
        template<class T>
        bool operator()(const T& left, const T& right) const {
            return ((*left) <( *right));
        }
    };
    
    std::priority_queue<MyClass*, vector<MyClass*>, ptr_less > que; 
    
    0 讨论(0)
提交回复
热议问题