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

前端 未结 4 1653
耶瑟儿~
耶瑟儿~ 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: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.

提交回复
热议问题