std::priority_queue<some_type, std::vector<some_type>, some_comparator> A;
std::priority_queue<some_type, std::vector<some_type>, some_comparator> B;
How can I merge these priority queues A and B based on same comparator. I tried to find builtin function but could not find any.
Answer: Yes. Priority Queue allows duplicate values.
PriorityQueue allows duplicates. So if you want to avoid that, you need to implement your own version of Queue. You can find very elegant way, how to do that in "Effective Java", page 85.
If two elements in a priority queue have the same priority, they will be arranged using the FIFO principle.
The simplest way is to simply move objects from one queue to another:
while(!B.empty()) {
A.push(B.top());
B.pop();
}
There might exist a more efficient method, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With