std::priority_queue默认为最大堆,通过第三个模板参数传入满足严格弱序的比较器可实现最小堆或自定义排序;例如std::priority_queue即为最小堆。

std::priority_queue 默认是最大堆,但通过自定义比较器可以实现最小堆或任意排序逻辑。关键在于传入第三个模板参数——一个满足严格弱序(strict weak ordering)的比较类型。
基础用法:默认最大堆
默认情况下,std::priority_queue
// 顶部始终是最大值 使用 std::greater std::priority_queue 两种常用方式: 立即学习“C++免费学习笔记(深入)”; 如果坚持用 lambda,需配合 std::function,但会失去编译期优化且性能略差: auto cmp = [](const int& a, const int& b) { return a > b; }; 实际项目中更推荐用函数对象或重载 operator
std::priority_queue
max_heap.push(3); max_heap.push(1); max_heap.push(4);
std::cout
实现最小堆:传入 std::greater
min_heap.push(3); min_heap.push(1); min_heap.push(4);
std::cout
自定义结构体排序:重载 operator
struct Task {
int id;
int priority;
bool operator
return priority > other.priority; // 小 priority 值优先级更高 → 实现最小堆语义
}
};
std::priority_queue
struct CompareTask {
bool operator()(const Task& a, const Task& b) const {
return a.priority > b.priority; // 返回 true 表示 a 应该排在 b 后面(即 b 优先级更高)
}
};
std::priority_queueLambda 不可直接用作模板参数,但可用 std::function 包装(不推荐,有开销)
std::priority_queue










