c++ - std::priority_queue<> 什么时候进行 self 排序?

标签 c++ algorithm stl computer-science priority-queue

我想知道什么时候 C++ STL priority_queue自行排序。我的意思是它insert当你push中的项目,或者当你peek时,它会自行排序并给你最高优先级的项目吗?或 pop出来?我问这个是因为我的 priority_queue<int>将包含一个可能有值更新的数组的索引,我希望它在我执行 pq.top(); 时更新.

#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;

int main() {
  priority_queue<int> pq;
  pq.push(2);
  pq.push(5); //is the first element 5 now? or will it update again when I top() or pop() it out?
  return 0;
}

谢谢。

最佳答案

工作是在push()pop()期间完成的,调用底层的堆修改函数(push_heap()pop_heap())。 top() 需要常数时间。

关于c++ - std::priority_queue<> 什么时候进行 self 排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7801438/

相关文章:

c++ - 在字符数组上使用 next_permutation

c++ - c++ std_lib_facilities.h 还在用吗?

c++ - 我在我的程序中使用 ffmpeg 库来录制视频和音频

c++ - 记忆化和朴素算法 - 2 个不同的答案

c++ - 我应该学习使用 C++ STL 容器而不是构建它们吗?

algorithm - 分词递归解的时间复杂度?

java - 创建棋盘图案

c++ - 如何从 std::map 中检索所有键(或值)并将它们放入 vector 中?

C++ 如何将 STL 列表传递给函数

c++ - 在迭代 map 时向/从 map 添加/删除元素是否安全