I am writing a class which has three priority queues as private members.
class Foo {
...
...
private:
// I am fine with using pointers instead if it helps.
std::priority_queue<int> first; // min heap.
std::priority_queue<int> second; // max heap.
std::priority_queue<int> third; // min heap.
};
Now I require first
and third
to start as min heaps
and second
as a max heap
. As part of the functionality of my class I need to do the following:
- Move
second
tofirst
. Ideally this is achieved through lowest amount of copying. The underlying vector should just be moved. Additionallyfirst
should now behave like amax heap
. - Move
third
tosecond
. This meanssecond
should now behave like amin heap
. - Since
third
's contents have been moved tosecond
, it should be empty. I would like to either allocate a new underlying vector or re-usefirst's
underlying vector (it doesn't need it any more. Additionally third should now be amax heap
.
I need to perform this cycle (max -> min and min -> max) an unknown number of times.
I am struggling to do this with std::priority_queue
since the Comparator is a template argument which means I cannot change it at run time. This is preventing me from turning a min heap
into a max heap
.
So my questions are:
- Is there a way I could possibly bend
std::priority_queue
to do my bidding without making it extremely ugly? - If not then could I perhaps re-structure my class to do the same thing, but still use
std::priority_queue
? - Otherwise could I maybe re-use most of the
heapify
logic in the std library to achieve this?