Senior 5 min · March 06, 2026

C++ priority_queue Comparator Gotcha — Order Reversal

A comparator that returned true for high priority silently reversed task order in production, delaying payments.

N
Naren · Founder
Plain-English first. Then code. Then the interview question.
About
 ● Production Incident 🔎 Debug Guide
Quick Answer
  • Container adapter wrapping a binary heap for O(log n) push/pop and O(1) top access
  • Default max-heap; flip to min-heap with std::greater (must also specify container type)
  • Custom comparators require 'return true if first arg should come OUT LATER' — opposite of std::sort
  • No iteration, no random access, no decrease-key — use std::set if you need those
  • Top performance: O(log n) extraction beats O(n log n) full sort for streaming max/min problems
  • Production trap: empty top()/pop() is UB, not an exception — always guard with empty()
Plain-English First

Imagine a hospital emergency room. Patients don't get seen in the order they arrived — the most critically injured person jumps to the front, no matter when they walked in. That's exactly what a priority queue does for your data: every time you ask 'who's next?', it hands you the most important element, not the oldest one. The queue keeps itself sorted automatically so you never have to think about ordering — you just push items in and pop the highest-priority one out.

Every program eventually has to answer the question 'what should I do next?' Sometimes the answer is simple — first in, first out. But real systems are messier than that. A CPU scheduler can't treat a kernel interrupt the same as a background file sync. Dijkstra's shortest-path algorithm needs to always expand the cheapest node next. A live leaderboard needs the top score instantly. In all these cases, a plain queue or sorted vector is either too slow, too manual, or both. That's the gap the priority queue was designed to fill.

The STL's priority_queue adapter sits on top of a heap data structure, giving you O(log n) insertions and O(1) access to the top element — without you ever writing a single line of heap maintenance code. It abstracts away the messy 'sift-up / sift-down' logic that makes heaps notoriously tricky to implement correctly. You get the speed of a heap with the simplicity of a queue interface.

By the end of this article you'll know exactly how priority_queue works under the hood, how to flip it from max-heap to min-heap, how to feed it custom objects with your own comparison logic, and — critically — when NOT to use it. You'll also walk away with clean, copy-paste-ready patterns for the three most common real-world use cases.

How priority_queue Works Under the Hood — Not Magic, Just a Heap

Before you touch a single line of code, it's worth understanding what you're actually using. std::priority_queue is a container adapter — it doesn't store data in its own structure. By default it wraps a std::vector and reorganises it as a binary max-heap. Every time you push an element, it gets placed at the end of the vector and then bubbled up to its correct position. Every time you pop, the root (the maximum element) is removed and the heap is restructured in O(log n) time.

This matters because it explains the constraints: you can only ever see or remove the top element. There's no iteration, no random access, no searching. If you need any of those, a priority queue is the wrong tool — use a std::set or std::multiset instead.

The default underlying container is std::vector, but you can swap it for std::deque. You'd almost never need to, but it's good to know the flexibility exists. The three template parameters are: the element type, the underlying container type, and the comparator. Most of the time you only need the first one.

BasicPriorityQueue.cppCPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <queue>      // required for priority_queue
#include <string>

namespace io_thecodeforge {
    void demonstrateMaxHeap() {
        // Default: max-heap — the largest integer sits at the top
        std::priority_queue<int> emergencyRoom;

        // Push patients with severity scores (higher = more critical)
        emergencyRoom.push(3);   // mild sprain
        emergencyRoom.push(9);   // cardiac arrest
        emergencyRoom.push(1);   // papercut
        emergencyRoom.push(7);   // broken leg
        emergencyRoom.push(5);   // moderate burn

        std::cout << "Treating patients in order of severity:\n";

        while (!emergencyRoom.empty()) {
            // top() is O(1), pop() is O(log n)
            std::cout << "  Severity " << emergencyRoom.top() << " treated.\n";
            emergencyRoom.pop();
        }
    }
}

int main() {
    io_thecodeforge::demonstrateMaxHeap();
    return 0;
}
Output
Treating patients in order of severity:
Severity 9 treated.
Severity 7 treated.
Severity 5 treated.
Severity 3 treated.
Severity 1 treated.
Watch Out: top() on an Empty Queue is Undefined Behaviour
Calling top() or pop() on an empty priority_queue doesn't throw an exception — it causes undefined behaviour (usually a crash or silent data corruption). Always guard with empty() first, especially in loops that drain the queue.
Production Insight
The UB on empty access is the #1 reason priority_queue bugs go unnoticed until production pressure hits.
A common pattern: drain loop assumes at least one element exists — if queue is empty, crash.
Rule: always check empty() before any top()/pop() call, even if you 'know' the queue isn't empty.
Key Takeaway
priority_queue is a max-heap by default; top() is O(1), push/pop O(log n).
The container adapter pattern means no iteration — understand the limitations before committing.
Guard every top()/pop() with empty() — UB is silent and lethal.

Building a Min-Heap — Flipping Priority for Shortest Path and K-Smallest Problems

The default max-heap is great for 'give me the biggest', but a huge class of problems needs the opposite: Dijkstra's algorithm always expands the cheapest unvisited node; a 'K smallest elements' problem needs constant access to the minimum. This is where beginners often reach for sorting, which costs O(n log n) upfront and doesn't help as new elements arrive dynamically.

To create a min-heap, you pass std::greater<T> as the third template argument. The syntax is a mouthful at first, but you only need to memorise the pattern once. Notice you also have to spell out the underlying container (std::vector<int>) as the second argument — you can't skip it because C++ template arguments must be provided in order.

A practical shortcut many developers use is to push negated integers into a max-heap (-value) and negate again on the way out. It works, but it's a hack that breaks with unsigned types and confuses anyone reading the code later. Use std::greater — it's the idiomatic, safe approach.

The example below solves a classic interview problem: finding the K-th smallest element in a stream of numbers using a max-heap of size K.

MinHeapAndKthSmallest.cppCPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <queue>
#include <vector>
#include <functional> 

namespace io_thecodeforge {
    // Strategy: Use a max-heap of size K to track the smallest values seen.
    int findKthSmallest(const std::vector<int>& numbers, int k) {
        if (k <= 0 || k > numbers.size()) return -1;

        std::priority_queue<int> maxHeap;

        for (int val : numbers) {
            maxHeap.push(val);
            if (maxHeap.size() > k) {
                maxHeap.pop(); // Remove the largest among our K smallest
            }
        }
        return maxHeap.top();
    }

    void runMinHeapDemo() {
        // Explicit min-heap declaration
        std::priority_queue<int, std::vector<int>, std::greater<int>> minPq;
        
        for(int x : {10, 2, 8, 1, 5}) minPq.push(x);

        std::cout << "Min-heap output: ";
        while(!minPq.empty()) {
            std::cout << minPq.top() << " ";
            minPq.pop();
        }
    }
}

int main() {
    io_thecodeforge::runMinHeapDemo();
    return 0;
}
Output
Min-heap output: 1 2 5 8 10
Pro Tip: Use 'using' Aliases to Tame the Ugly Min-Heap Syntax
The full type std::priority_queue<int, std::vector<int>, std::greater<int>> is verbose. Declare 'using MinHeap = std::priority_queue<int, std::vector<int>, std::greater<int>>;' at the top of your file or inside the function. Your code becomes MinHeap pq; — clean, readable, and intent-revealing.
Production Insight
The negation trick for min-heap works but is fragile: unsigned types break, code reviewers get confused.
In production code, always use std::greater — it's explicit and doesn't silently cast types.
The K-th smallest with max-heap of size K uses O(K) memory; for large K, consider nth_element.
Key Takeaway
Min-heap requires std::greater<T> as third template argument, plus the container type.
Negation hack is a code smell — avoid in production.
Using aliases (using MinHeap = ...) improves readability significantly.

Custom Comparators — Prioritising Real Objects Like Tasks, Events, or Graph Nodes

Integer examples are tidy, but production code deals with structs and objects. Imagine a task scheduler where each task has a name, a priority level, and a deadline. You want to process higher-priority tasks first, and break ties by preferring the earlier deadline. No built-in comparator handles that — you need to define the ordering yourself.

You have three good options: a functor (a struct with operator()), a lambda, or overloading operator< on the struct itself. Functors are the most reusable and are what you'll see in most professional codebases. Lambdas are great for quick one-off comparators but require using decltype in the template, which is awkward. Overloading operator< is clean but couples the ordering logic to the type definition — problematic when you need different orderings in different contexts.

The comparator contract is the same as std::sort: return true if the left argument should come after the right (i.e., has lower priority). This is the opposite of what feels intuitive, and it's one of the top sources of off-by-one logic bugs with custom heaps. The example below shows a practical task scheduler with tie-breaking.

TaskScheduler.cppCPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <queue>
#include <string>
#include <vector>

namespace io_thecodeforge {
    struct Task {
        std::string title;
        int priority;   // Higher is more important
        int deadline;   // Lower hour is more urgent
    };

    struct TaskComp {
        bool operator()(const Task& a, const Task& b) {
            if (a.priority != b.priority) {
                return a.priority < b.priority; // Lower priority sinks
            }
            return a.deadline > b.deadline; // Later deadline sinks
        }
    };

    void runScheduler() {
        std::priority_queue<Task, std::vector<Task>, TaskComp> pq;
        pq.push({"Fix Bug", 10, 9});
        pq.push({"Email", 2, 12});
        pq.push({"Hotfix", 10, 8});

        while(!pq.empty()) {
            auto t = pq.top();
            std::cout << "Executing: " << t.title << "\n";
            pq.pop();
        }
    }
}

int main() {
    io_thecodeforge::runScheduler();
    return 0;
}
Output
Executing: Hotfix
Executing: Fix Bug
Executing: Email
Interview Gold: The Comparator Returns 'Should Come After', Not 'Is Less Than'
The comparator for priority_queue works like the comparator for a max-heap: return true if the first argument should be LOWER in the heap (i.e., come out LATER). This is the inverse of std::sort's comparator intuition. Mixing them up gives you a heap that silently runs backwards — no compile error, wrong output.
Production Insight
A functor comparator is reusable and testable — you can unit test it independently of the queue.
Lambdas with decltype can be tricky to declare type aliases for; prefer functors in shared code.
The reversed comparator contract is the #1 source of priority_queue bugs in production — always test pop order with 3 elements.
Key Takeaway
Custom comparators use the rule: return true if first arg should be LOWER priority (come out LATER).
Use functors for reuse, lambdas for one-off cases, operator< sparingly.
Test comparator behavior with a minimal example before relying on it.

Real-World Patterns — Dijkstra's Algorithm and the Merge K Sorted Lists Problem

Theory lands better with concrete applications. Two of the most important algorithms in computer science lean on a priority queue as their core data structure: Dijkstra's shortest path and the K-way merge.

In Dijkstra's algorithm, you need to always process the unvisited node with the smallest known distance. A min-heap makes that O(log n) per extraction instead of O(n) with a linear scan — the difference between a usable and an unusable graph algorithm on large inputs.

The K-way merge problem (merge K sorted lists into one sorted list) appears in database query engines, external sorting, and the implementation of merge sort itself. The trick: seed the heap with the first element from each list. Pop the minimum, advance the pointer in that list, push its next element. Repeat. The heap never holds more than K elements, so memory is O(K) and time is O(N log K) where N is total elements.

Below is a clean Dijkstra implementation on an adjacency list graph. It's the pattern you'll see in competitive programming and system design interviews alike.

DijkstraShortestPath.cppCPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <queue>
#include <vector>
#include <limits>

namespace io_thecodeforge {
    typedef std::pair<int, int> pii; // {distance, node}

    void dijkstra(int start, const std::vector<std::vector<pii>>& adj) {
        std::vector<int> dist(adj.size(), std::numeric_limits<int>::max());
        std::priority_queue<pii, std::vector<pii>, std::greater<pii>> pq;

        dist[start] = 0;
        pq.push({0, start});

        while(!pq.empty()) {
            int d = pq.top().first;
            int u = pq.top().second;
            pq.pop();

            if (d > dist[u]) continue;

            for (auto& edge : adj[u]) {
                int v = edge.first;
                int weight = edge.second;
                if (dist[u] + weight < dist[v]) {
                    dist[v] = dist[u] + weight;
                    pq.push({dist[v], v});
                }
            }
        }
        
        std::cout << "Node 1 distance: " << dist[1] << "\n";
    }
}

int main() {
    std::vector<std::vector<io_thecodeforge::pii>> adj(2);
    adj[0].push_back({1, 5});
    io_thecodeforge::dijkstra(0, adj);
    return 0;
}
Output
Node 1 distance: 5
Pro Tip: STL priority_queue Has No decrease-key — Use Lazy Deletion Instead
Unlike a Fibonacci heap, std::priority_queue can't update an existing element's priority. The standard workaround is to push a new entry with the updated distance and skip stale entries when they're popped (the 'if currentDist > shortestDist[node]: continue' guard). This is O(E log E) instead of O(E log V) but is simpler, cache-friendlier, and fast enough for most real problems.
Production Insight
Lazy deletion is the standard pattern for Dijkstra with STL — it's simpler and often faster due to cache behavior.
The skip-stale check (if d > dist[u]) is critical: without it, stale entries corrupt results.
For very dense graphs, a Fibonacci heap can be faster but adds complexity; profile before switching.
Key Takeaway
Use lazy deletion for Dijkstra with priority_queue: push new entries, skip stale ones.
K-way merge is memory-efficient (O(K)) and time-efficient (O(N log K)).
Know the trade-off: no decrease-key => O(E log E) vs O(E log V) with a dedicated heap.

When NOT to Use priority_queue — Identifying the Right Tool for the Job

As powerful as it is, priority_queue isn't the answer to every ordering problem. Here are the scenarios where you should reach for something else:

  • Need to iterate elements in order? priority_queue doesn't support iteration. If you need to traverse all elements without destroying the queue, use std::set or std::multiset — they maintain sorted order and support iteration.
  • Need to update an element's priority after insertion? No decrease-key or increase-key supported. You'd have to push a new entry and live with stale ones (lazy deletion). If updates are frequent, consider std::set with explicit removal and reinsertion, or a custom indexed heap.
  • Need to search for an arbitrary element? No search. If you need to find a value, use a balanced BST.
  • Need random access? No random access. Use std::vector + sort if you need index-based access.
  • Need to process elements in both ascending and descending order? Only one end is accessible. For two-ended priority queries, use a std::multiset or two heaps (min- and max-heap combination).

Knowing when NOT to use priority_queue is as important as knowing when to use it. It's a specialized tool: great for streaming max/min extraction, poor for everything else.

WhenToAvoid.cppCPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <queue>
#include <set>

namespace io_thecodeforge {
    void compareApproaches() {
        // This works — we only need the max
        std::priority_queue<int> maxQ;
        maxQ.push(5); maxQ.push(1); maxQ.push(9);
        std::cout << "Top: " << maxQ.top() << "\n"; // 9

        // This fails — we need to see all elements sorted without popping
        // maxQ doesn't support iteration

        // Instead, use std::multiset
        std::multiset<int> sortedSet = {5, 1, 9};
        std::cout << "All elements: ";
        for (int x : sortedSet) std::cout << x << " ";
        std::cout << "\n";

        // This also fails — we need to update priority of an element in the middle
        // priority_queue has no decrease-key

        // Workaround with std::set: remove and reinsert, but that's O(log n)
        auto it = sortedSet.find(5);
        if (it != sortedSet.end()) {
            sortedSet.erase(it);
            sortedSet.insert(3); // pretend this is an updated priority
        }
    }
}

int main() {
    io_thecodeforge::compareApproaches();
    return 0;
}
Output
Top: 9
All elements: 1 5 9
The Right Tool for Ordering Needs
  • Need iteration? Use std::set/multiset (O(log n) operations) or std::vector + sort (O(n log n)).
  • Need update priority? Use std::set (erase + insert) or a custom indexed heap (boost::fibonacci_heap).
  • Need both ends? Use two heaps (min and max) or std::multiset with begin() and rbegin().
  • Need random access? Use std::vector with sorting and binary search.
Production Insight
Using priority_queue when you need iteration is a common rework trigger — you'll end up popping all elements into a vector, which defeats the purpose.
In high-churn systems, the lack of decrease-key forces lazy deletion, which can inflate queue size dramatically if stale entries accumulate.
Always evaluate: if your algorithm accesses more than just the top, an indexed data structure will perform better.
Key Takeaway
priority_queue is optimal ONLY for repeated max/min extraction from a dynamic set.
For iteration, search, priority updates, or two-ended access, use std::set, std::vector, or a custom heap.
Choosing the wrong ordering data structure is a silent performance killer.
● Production incidentPOST-MORTEMseverity: high

Production Incident: Task Scheduler Processes All Tasks in Wrong Order

Symptom
The scheduler processed low-priority tasks (like log rotation) before high-priority tasks (like payment processing), leading to delayed payments and customer complaints. Logs showed no errors — every task executed, just in the wrong order.
Assumption
The team assumed the comparator worked like std::sort — return true if the first element should come FIRST. That's wrong for priority_queue.
Root cause
The custom comparator returned true when the first argument should have higher priority (i.e., come out first). In priority_queue, the comparator returns true if the first argument should be LOWER in the heap (come out LATER). The entire ordering was reversed with no compiler warning.
Fix
Reversed the comparator logic: changed 'return a.priority > b.priority' to 'return a.priority < b.priority' (or used std::greater if min-heap was desired). Also added a unit test with three elements and manually verified the pop order.
Key lesson
  • priority_queue comparator contract is the inverse of std::sort — test with a simple three-element case before relying on it.
  • Never skip unit tests for queue ordering; a silent reversal can go unnoticed in staging.
  • Add a static_assert or compile-time check for trivial types to verify comparator consistency.
Production debug guideSymptom → Action guide for common priority_queue failures4 entries
Symptom · 01
Queue returns elements in unexpected order (e.g., min-heap acting like max-heap or vice versa)
Fix
Check comparator: does it return true when first arg should be LOWER priority (come out later)? Test with three known elements (e.g., push 1, 2, 3) and verify pop order. Compare with std::greater / std::less for primitive types.
Symptom · 02
Segfault or garbage output when accessing top or pop
Fix
Add empty() check before each top()/pop() call. If crash occurs only intermittently, instrument with a mutex if multiple threads access the queue — priority_queue is not thread-safe.
Symptom · 03
Memory usage grows unboundedly even though queue size is limited
Fix
Check if you're accidentally pushing duplicate entries (lazy deletion without skipping stale ones). Use a separate 'valid' flag or a timestamp to identify outdated entries. Monitor queue size in production.
Symptom · 04
Frequent heap operations cause slowdowns under high throughput
Fix
Profile push/pop times. If O(log n) is too heavy, consider batching updates or switching to a different data structure (e.g., std::multiset for indexed updates). For high-churn scenarios, evaluate pairing heap or Fibonacci heap from Boost.
★ Quick Debug Cheat Sheet: priority_queueRapid diagnosis of the three most common priority_queue failures
Elements come out in reverse priority order
Immediate action
Stop using the queue and isolate the comparator logic in a minimal test
Commands
std::priority_queue<int, std::vector<int>, std::greater<int>> pq; // test with min-heap
for (int x : {5,1,9}) { pq.push(x); } while (!pq.empty()) { std::cout << pq.top() << ' '; pq.pop(); } // expect 1 5 9
Fix now
Fix comparator: return true if first arg should have LOWER priority. For a custom type, ensure operator< is consistent with std::less behavior
Crash on top() or pop()+
Immediate action
Check for empty condition immediately before each call
Commands
if (!pq.empty()) { /* access top/pop */ }
In loops: while (!pq.empty()) { auto t = pq.top(); pq.pop(); }
Fix now
Wrap all accesses in empty() guard
Queue never empties or grows without bound+
Immediate action
Check for missing pop() after top(), or duplicate pushes from lazy deletion
Commands
std::cout << "Queue size: " << pq.size(); // verify expected size
If using lazy deletion: ensure you skip stale entries via a version counter or a 'valid' flag when popping
Fix now
Add a pop() after each top() that processes the element, and implement stale-entry skipping
Feature / Aspect
Feature / Aspectstd::priority_queuestd::set / std::multiset
Underlying structureBinary heap (via vector)Red-black tree
Access top elementO(1) via top()O(1) via begin() or rbegin()
InsertionO(log n)O(log n)
Remove top elementO(log n)O(log n)
Remove arbitrary elementNot supportedO(log n)
Iterate all elementsNot supportedFully supported (sorted order)
Duplicate elementsAllowedmultiset allows; set does not
Custom orderingComparator functor / lambdaComparator functor / lambda
Memory overheadLow (contiguous vector)Higher (tree node pointers)
Best forAlways process max/min nextSearch, range queries, update priority

Key takeaways

1
priority_queue is a max-heap by default
the largest element is always at top(). For min-heap behaviour you need std::greater<T> as the third template parameter, and you must also supply the container type (std::vector<T>) as the second.
2
top() is O(1), push() and pop() are O(log n)
this is what makes priority_queue the right tool for streaming data where you repeatedly need the current maximum or minimum without re-sorting the whole collection.
3
Custom comparators must follow the 'return true if left argument should come out LATER' contract
the inverse of the std::sort comparator intuition. Getting this backwards gives you a silently reversed heap.
4
STL priority_queue has no decrease-key operation and no iteration. If you need to update an element's priority or search the contents, use std::set instead. For Dijkstra with STL, use the lazy deletion pattern.
5
Choosing the right ordering data structure is critical
priority_queue only for repeated max/min extraction; std::set/multiset for sorted iteration and updates; std::vector + sort for static ordering.
6
Always guard top()/pop() with empty()
undefined behaviour on empty queue is a silent production killer.

Common mistakes to avoid

4 patterns
×

Calling top() or pop() without checking empty()

Symptom
Undefined behaviour: segfault or silent garbage read, not a clean exception. Occurs when draining loop assumes non-empty queue.
Fix
Always use if (!pq.empty()) before any top()/pop() call. Prefer while (!pq.empty()) drain loops.
×

Getting the custom comparator backwards

Symptom
Queue outputs elements in reversed priority order with no compile error. E.g., max-heap behaves like min-heap.
Fix
Remember: comparator returns true if first arg should be LOWER in heap (come out LATER). Test with three known elements before relying on it.
×

Trying to modify an element's priority after insertion

Symptom
Heap invariant silently corrupted — incorrect ordering or missing elements. No compiler error, only subtle runtime misbehaviour.
Fix
Use lazy deletion: push a new entry with updated priority, and skip stale entries via a version counter or visited flag when popped.
×

Using priority_queue when iteration or search is needed

Symptom
Workarounds (like popping all elements into a vector) lead to performance degradation and code complexity.
Fix
Switch to std::set/multiset if you need iteration, search, or priority updates. Use std::vector + sort if order changes rarely.
INTERVIEW PREP · PRACTICE MODE

Interview Questions on This Topic

Q01SENIOR
Explain the internal working of push() and pop() in std::priority_queue....
Q02SENIOR
How can you implement a Median Finder from a data stream using two prior...
Q03SENIOR
Why is std::vector preferred over std::list as the underlying container ...
Q04SENIOR
Can you perform a 'Top K Frequent Elements' search in O(N log K) time? W...
Q05SENIOR
Describe the 'Lazy Deletion' technique used in Dijkstra's algorithm with...
Q01 of 05SENIOR

Explain the internal working of push() and pop() in std::priority_queue. What are the average and worst-case time complexities, and how is the heap property maintained during a pop operation?

ANSWER
push() places the new element at the end of the underlying container (default vector), then performs a 'sift-up' or 'bubble-up' operation: it compares the new element with its parent and swaps if the child has higher priority. This runs O(log n) worst-case. pop() removes the top element by swapping it with the last element, then performing a 'sift-down' or 'heapify' from the root to restore the heap property, also O(log n). The heap property (parent >= children for max-heap) is maintained during both operations. The underlying container is not sorted; only the heap ordering is guaranteed.
FAQ · 7 QUESTIONS

Frequently Asked Questions

01
How do I create a min-heap with priority_queue in C++?
02
Can I use priority_queue with my own custom struct?
03
Is std::priority_queue thread-safe?
04
What happens if I push two elements with the same priority?
05
Why is there no 'bottom()' or way to see the lowest priority item in a max-heap?
06
Can I use priority_queue for a K-way merge of sorted lists?
07
How can I debug a priority_queue that produces wrong ordering?
🔥

That's STL. Mark it forged?

5 min read · try the examples if you haven't

Previous
STL Iterators in C++
7 / 11 · STL
Next
STL Pairs and Tuples in C++