Priority Queues using std::priority_queue

Priority Queue with Unique Elements

How can I ensure that a priority queue contains only unique elements?

Illustration representing computer hardware

To ensure that a priority queue contains only unique elements, you can use a combination of a priority queue and a set data structure. The set will keep track of the unique elements, while the priority queue will maintain the heap property based on the priority of the elements.

Here's an example of how you can implement a priority queue with unique elements:

#include <iostream>
#include <queue>
#include <unordered_set>

int main() {
  std::priority_queue<int> pq;
  std::unordered_set<int> uniqueElements;

  auto insertElement = [&](int element) {
    if (uniqueElements.insert(element).second) {
      pq.push(element);
    }
  };

  insertElement(10);
  insertElement(30);
  insertElement(20);
  insertElement(30); // Duplicate not inserted
  insertElement(40);

  while (!pq.empty()) {
    std::cout << pq.top() << " ";
    pq.pop();
  }
}

In this example, we create a std::priority_queue<int> to store the elements and a std::unordered_set<int> to keep track of the unique elements.

We define a lambda function insertElement that takes an element as input. Inside the lambda function, we attempt to insert the element into the uniqueElements set using the insert() method. The insert() method returns a pair, where the second element is a boolean indicating whether the insertion was successful (i.e., the element was not already present in the set).

If the insertion into the set is successful, we push the element into the priority queue using pq.push(). This ensures that only unique elements are added to the priority queue.

When you run this code, it will output:

40 30 20 10

As you can see, the duplicate element 30 is not inserted into the priority queue.

By using a set to keep track of the unique elements, we can efficiently check if an element is already present before inserting it into the priority queue. The time complexity of insertion into both the set and the priority queue is O(log n), where n is the number of elements.

Note that using a set to maintain uniqueness adds some overhead compared to a regular priority queue, so use this approach only if maintaining uniqueness is a requirement for your specific use case.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved