Search code examples
c++pointerspriority-queue

Change sorting function of priority queue during runtime


I have a std::priority_queue with a custom sorting function. At a point in the program, I want to reorganize the queue using a different function.

Is this possible? Or can I make a pointer to the queue that I can point to a differently sorted queue when needed?


Solution

  • That's not possible as the Compare function given to the priority_queue is a template type, this means that the Compare itself belongs to that type.

    These:

    std::priority_queue<int, std::vector<int>, std::less<int>>;
    std::priority_queue<int, std::vector<int>, std::greater<int>>;
    

    While looking similar are 2 different types.

    What you can do is adapt your Compare function to look at some external state and sort based on that.