我想知道什么时候 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/