温馨提示×

c++ priority_queue的底层数据结构

c++
小樊
82
2024-09-04 19:08:51
栏目: 编程语言

C++中的priority_queue是一个容器适配器,它提供了常数时间查找最大元素(在std::greater比较器下)和对数时间删除最大元素的能力

如果你想要自定义比较函数或者使用其他类型的底层容器,可以在priority_queue的模板参数中指定。例如:

#include<queue>
#include<vector>
#include<functional>

// 使用vector作为底层容器,并使用自定义比较函数
typedef std::priority_queue<int, std::vector<int>, std::greater<int>> CustomPriorityQueue;

这里我们使用了std::greater<int>作为比较函数,所以CustomPriorityQueue将会保存最小元素在顶部。

0