首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优先级队列作为最小堆c++与python中的heapq (如何将heapq.heappop(openList)转换为c++?)

优先级队列作为最小堆c++与python中的heapq (如何将heapq.heappop(openList)转换为c++?)
EN

Stack Overflow用户
提问于 2020-08-30 10:18:09
回答 1查看 169关注 0票数 0

我有以下python代码:

代码语言:javascript
复制
    import heapq
    heapq.heappush(openList, currentSearchNode)
    #NOTE List of nodes that have been checked
    closedList = []
    while openList:
        #NOTE Pop the lowest fscore (to-go + been from or gScore + hScore) and set it as current
        currentSearchNode = heapq.heappop(openList)
...

我需要把它转换成C++14,我试过了:

代码语言:javascript
复制
#include <functional>
#include <queue>
priority_queue <Node, vector<Node>, greater<Node>> min_heap;
vector<Node> openList, closeList;
Node currentNode = Node(start, euclidean(start, end), 0);
min_heap.emplace(openList, currentNode);
while (!openList.empty()) {
    currentNode = min_heap.pop(openList);
...
}

在Visual Studio中弹出红色的唯一问题是你可以看到的这一行currentNode = min_heap.pop(openList);,它说,pop的参数太多了。怎样做才是正确的呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-30 10:41:12

如下所示如何?

代码语言:javascript
复制
while (!min_heap.empty()) {
     currentNode = min_heap.top(); // sets the top small (since std::greater used)element to currentNode;
     
     /* do something with currentNode */

     min_heap.pop(); // pops the element from container  
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63653383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档