我想知道C++ STL中的priority_queue是什么时候排序的。我的意思是,在使用push方法添加元素时,它是否会将元素插入到正确的位置,还是在peek或pop方法调用时自动排序并返回最高优先级的元素。我问这个问题是因为我的priority_queue中包含了数组索引,而数组可能会被更新,我希望当我调用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;
}
感谢您的选择。
map
一样需要一个比较谓词。如果你提供一个在每次比较时打印到控制台的比较谓词(例如),你将会实时看到它何时被调用(以及哪些值)。 - Matthieu M.