【发布时间】:2018-03-09 08:13:30
【问题描述】:
是否可以弹出max,push一个新元素,然后调用push_heap,维护堆,只调用一次bubble down算法?
// What I think is correct:
heap.push_back(new_element);
std::push_heap(heap.begin(), heap.end());
std::pop_heap(heap.begin(), heap.end());
heap.pop_back();
// What I hope is correct:
heap.pop_back();
heap.push_back(new_element);
std::push_heap(heap.begin(), heap.end());
删除最大值并将新元素推送到堆中是否“安全”?我已经测试过了,似乎是这样。我有理由不这样做吗?
非常相关的问题: How to change max element in a heap in C++ standard library?
【问题讨论】:
-
最大的元素会不会在前面,导致'pop_back()'不正确?
-
是的 - 谢谢。我的“测试”有一个我没有看到的额外 pop_heap 语句。