Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure std::priority_queue to ignore duplicates?

How can I configure std::priority_queue to ignore duplicates?

When I add a key that is already contained then this new one should be ignored. (In my case, the priority for the old and the new one will always be exactly the same.)

Complexity-wise it should not make a difference: It will try to insert at the appropriate location, find the existing one there and do nothing. The question is just if std::priority_queue is configurable in that way.

like image 673
Frank Avatar asked May 10 '11 18:05

Frank


People also ask

How do I avoid duplicates in priority queue?

A PriorityQueue in Java does not have any restriction with regard to duplicate elements. If you want to ensure that two identical items are never present in the priority queue at the same time the simplest way would be to maintain a separate Set in parallel with the priority queue.

Does priority queue allow duplicates C++?

Yes, in C++ priority_queue, we may have duplicate values.

Does priority queue accept duplicates?

Answer: Yes. Priority Queue allows duplicate values.

Does stack allow duplicates in Java?

Stack<T> accepts null as a valid value for reference types and allows duplicate elements.


1 Answers

You can implement a priority_queue out of an STL set.

Implementing a priority queue that can be iterated over in C++

like image 140
Aater Suleman Avatar answered Oct 12 '22 00:10

Aater Suleman