C++ 最小堆提取2个最小元素

C++ 最小堆提取2个最小元素,c++,C++,我有一个最小堆 priority_queue<double, vector<double>, greater<double>> min_heap; // push vector values to heap for (const auto& e : rand) min_heap.push(e); 优先级队列最小堆; //将向量值推送到堆 用于(常数自动和电气:兰特) 最小堆压(e); 我怎样才能在O(n)时间内得到这个堆中的2个最小值,比如

我有一个最小堆

priority_queue<double, vector<double>, greater<double>> min_heap;
// push vector values to heap
for (const auto& e : rand)
    min_heap.push(e);
优先级队列最小堆;
//将向量值推送到堆
用于(常数自动和电气:兰特)
最小堆压(e);
我怎样才能在
O(n)
时间内得到这个堆中的2个最小值,比如说只使用一个循环

注意。

一旦堆形成,您可以在
O(logn)
时间内完成。您可以通过一个pop和两个查询来完成

int min1 = min_heap.top(); //get the minimum element
min_heap.pop(); //pop the minimum element from the heap
int min2 = min_heap.top(); //get the second minimum element(the new top)
min_heap.push(min1); //push the old 'top' to restore the heap to its old state
查看这一点会有所帮助:

一旦堆形成,就可以在
O(logn)
时间内完成。您可以通过一个pop和两个查询来完成

int min1 = min_heap.top(); //get the minimum element
min_heap.pop(); //pop the minimum element from the heap
int min2 = min_heap.top(); //get the second minimum element(the new top)
min_heap.push(min1); //push the old 'top' to restore the heap to its old state

查看此信息将有所帮助:

您知道如何使用优先级队列吗?“你知道各种各样的方法是什么,它们的复杂性是什么吗?”穆因达克说得不太实话。从未明确使用过它们,现在看来我需要学习。你知道如何使用优先级队列吗?“你知道各种各样的方法是什么,它们的复杂性是什么吗?”穆因达克说得不太实话。从未明确使用过,现在看来我需要学习。