This project implements a custom generic priority queue container in modern C++, similar to std::priority_queue, with configurable:
- value type
- underlying container
- comparison strategy
It also includes a small demo in main.cpp.
priority_queue.hpp- declaration ofpriorityQueuetemplate class and deduction guidespriority_queue.impl.hpp- implementation of allpriorityQueuemember functionsAlgorithm.hpp- declarations of heap utility algorithms in namespaceHeapAlgorithm.impl.hpp- implementation of heap algorithms (makeHeap,pushHeap,popHeap,Heapify)main.cpp- usage examples and output demo
The design is split into two layers:
- Container layer (
priorityQueue)- Public API:
push,pop,top,empty,size,emplace,swap - Owns data (
Container c) and comparison object (Compare cmp)
- Public API:
- Algorithm layer (
Heapnamespace)- Stateless heap procedures that operate on iterator ranges
- Called internally by
priorityQueueto maintain heap invariants
- Constructor from container/range ->
Heap::makeHeap(...) push/emplace-> append element ->Heap::pushHeap(...)pop->Heap::popHeap(...)-> remove last elementtop-> returnsc.front()(heap root)
flowchart TD
A[main.cpp] --> B[priorityQueue<T, Container, Compare>]
B --> C[Container c]
B --> D[Compare cmp]
B --> E[Heap::makeHeap]
B --> F[Heap::pushHeap]
B --> G[Heap::popHeap]
G --> H[Heap::Heapify]
H --> I[Heap::getLeft]
H --> J[Heap::getRight]
priority_queue.hppincludesAlgorithm.hpppriority_queue.impl.hppdepends onHeapfunctions declared inAlgorithm.hppAlgorithm.hppincludesAlgorithm.impl.hpp(template implementation pattern)main.cppdepends only onpriority_queue.hpp(plus standard library headers)
Simple compile command:
g++ main.cppRun:
- Linux:
./a.out- Windows:
./a.exepriorityQueue<int> maxQ;
maxQ.push(10);
maxQ.push(3);
maxQ.push(7);
std::cout << maxQ.top() << '\n'; // highest-priority element
maxQ.pop();