C++ C++;STL pop_堆不工作
我试图使用heap来解决“合并K个列表”的问题,即合并K个排序的链表并将其作为一个排序的列表返回。通常,我创建一个最小堆来存储所有列表节点,并使用预定义函数LessThanLinkedList()进行比较。 但我发现第62行和第75行中的pop_heap()操作根本不起作用。它不会移除堆的顶部,尽管我使用了预定义的比较函数作为参数。下面是我的代码。我使用VisualStudio2010作为IDE。有人知道原因吗?非常感谢你的帮助C++ C++;STL pop_堆不工作,c++,stl,heap,C++,Stl,Heap,我试图使用heap来解决“合并K个列表”的问题,即合并K个排序的链表并将其作为一个排序的列表返回。通常,我创建一个最小堆来存储所有列表节点,并使用预定义函数LessThanLinkedList()进行比较。 但我发现第62行和第75行中的pop_heap()操作根本不起作用。它不会移除堆的顶部,尽管我使用了预定义的比较函数作为参数。下面是我的代码。我使用VisualStudio2010作为IDE。有人知道原因吗?非常感谢你的帮助 #include <stdio.h> #include
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <list>
#include <numeric>
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
using namespace std;
class Solution {
public:
static bool LessThanLinkedList( const ListNode * l1, const ListNode * l2)
{
return( l1->val > l2->val );
}
ListNode *mergeKLists(vector<ListNode *> &lists) {
int idx;
bool ball_list_null;
ListNode * pNode;
ListNode *new_head;
ball_list_null = true;
for( idx = 0; idx < lists.size(); idx++ )
{
if( NULL != lists[idx] )
{
ball_list_null = false;
break;
}
}
if( true == ball_list_null )
return(NULL);
vector< ListNode* > list_heap;
for( idx = 0; idx < lists.size(); idx++ )
{
if( NULL != lists[idx] )
{
pNode = lists[idx];
while( NULL != pNode )
{
list_heap.push_back( pNode );
pNode = pNode->next;
}
}
}
make_heap( list_heap.begin(), list_heap.end(), LessThanLinkedList );
if(list_heap.size() > 0)
{
new_head = list_heap[0];
pop_heap( list_heap.begin(), list_heap.end(), LessThanLinkedList );//not work
}
if( list_heap.size() == 0 )
{
new_head->next = NULL;
}
else
{
pNode = new_head;
while( list_heap.size() >0 )
{
pNode->next = list_heap[0];
pop_heap( list_heap.begin(), list_heap.end(), LessThanLinkedList ); // not work
pNode = pNode->next ;
}
pNode->next = NULL;
}
return( new_head );
}
};
void main()
{
Solution xpfsln;
ListNode *l1,*l2,*l3,*l4,*l5,*l6,*l7,*head;
l1 = new ListNode(1);
l2 = new ListNode(2);
l3 = new ListNode(3);
l1->next = l2;
l2->next = l3;
l3->next = NULL;
vector<ListNode *> list_vec;
list_vec.push_back(l1);
head = xpfsln.mergeKLists( list_vec );
}
#包括
#包括
#包括
#包括
#包括
#包括
结构列表节点{
int-val;
ListNode*下一步;
ListNode(intx):val(x),next(NULL){}
};
使用名称空间std;
类解决方案{
公众:
静态bool LessThanLinkedList(常量ListNode*l1,常量ListNode*l2)
{
返回(l1->val>l2->val);
}
ListNode*合并列表(向量和列表){
int-idx;
布尔球列表为空;
ListNode*pNode;
ListNode*新的头;
ball_list_null=true;
对于(idx=0;idx列表\u堆;
对于(idx=0;idxnext;
}
}
}
生成堆(list\u heap.begin()、list\u heap.end()、LessThanLinkedList);
if(list_heap.size()>0)
{
新建_头=列表_堆[0];
pop_heap(list_heap.begin()、list_heap.end()、LessThanLinkedList);//不工作
}
if(list_heap.size()==0)
{
新建头->下一步=空;
}
其他的
{
pNode=新的_头;
while(list_heap.size()>0)
{
pNode->next=list_heap[0];
pop_heap(list_heap.begin()、list_heap.end()、LessThanLinkedList);//不工作
pNode=pNode->next;
}
pNode->next=NULL;
}
返回(新头);
}
};
void main()
{
溶液xpfsln;
列表节点*l1、*l2、*l3、*l4、*l5、*l6、*l7、*head;
l1=新的ListNode(1);
l2=新的ListNode(2);
l3=新的ListNode(3);
l1->next=l2;
l2->next=l3;
l3->next=NULL;
向量列表;
列表向量向后推(l1);
head=xpfsln.mergeKLists(列表向量);
}
pop\u heap
不会从容器中删除元素。它不能,因为它甚至不能访问容器,只能访问其元素。它所做的(当然假设[begin,end)
形成一个有效的非空堆)是重新排列元素,使堆的第一个元素移动到范围的最后一个元素,并离开[开始,结束-1)
作为有效堆。如果要从容器中实际删除元素,只需在调用pop\u heap
后擦除容器的最后一个元素(例如,通过调用pop\u back()
)
pop_heap( list_heap.begin(), list_heap.end(), LessThanLinkedList );
list_heap.pop_back();
非常感谢,非常宝贵的回答。它似乎与原来的stack pop函数不一样。我可以理解设计特点,但会带来一些不一致。