C++ Priority Queue
Use a priority queue when only the next extreme element matters, and choose the comparison direction deliberately.
How it works
std::priority_queue maintains an element with highest priority at top according to its comparator. The default makes the largest value appear first. Supplying greater instead exposes the smallest value, useful for the earliest departure minute. The adaptor does not provide sorted iteration; repeatedly removing top yields a priority sequence while consuming the queue. When equal priorities need a stable order, include an explicit tie-break key rather than assuming insertion order is preserved.
Run it locally
Use a C++20 compiler and matching standard library in a separate lab folder. The command uses c++; substitute g++, clang++ or zig c++ for your toolchain. C++ does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed.
c++ -std=c++20 -Wall -Wextra -Wpedantic -Werror priority-queue.cpp -o lessonOn Windows use -o lesson.exe and run .\lesson.exe in PowerShell. On Unix-like systems run ./lesson. Thread examples may need -pthread on your platform.
Download the complete C++ source. Run file labs in a private empty directory, away from existing reports.
Example
#include <iostream>
#include <queue>
#include <vector>
#include <functional>
int main() {
std::priority_queue<int, std::vector<int>, std::greater<int>> departures;
departures.push(35); departures.push(10); departures.push(25);
while (!departures.empty()) {
std::cout << "Departure: " << departures.top() << '\n';
departures.pop();
}
}
Expected result
Departure: 10
Departure: 25
Departure: 35
The smallest minute is selected each time. Push and removal require logarithmic work, while observing top is constant-time. If the application needs arbitrary updates or removal of a known job, another representation or a deliberate stale-entry strategy may be needed. Do not retain a reference to top across mutations that can move its storage.
Consult the WG21 C++ working group and the C++ Core Guidelines for language references and design guidance. Check the selected standard version and your implementation’s library support.