I am using struct minHeap to generate a min heap using priority_queue .And function comp to print numbers in reverse order using sort function given in STL . Now my doubt is tha
You can use a functor in sort()
, no problem at all:
sort(arr , arr+4 ,minHeap());
Maybe your problem was that you were just using the class name (minHeap
) instead of an instance of the functor. minHeap()
is a call to the constructor, not to operator()
.
As for priority_queue
, it is specified as follows:
template < class T, class Container = vector,
class Compare = less > class priority_queue;
Therefore, you need a class name (as opposed to an instance) for the third template argument. If you want to use a function, you must use a pointer to a function type as third template argument and then pass the function pointer in the constructor:
priority_queue , bool (*)(int a, int b) > b(&comp);