简体   繁体   English

C ++ STL pop_heap不起作用

[英]C++ STL pop_heap not work

I am trying to use heap to solve the problem "merge K lists" which is merging k sorted linked lists and return it as one sorted list. 我正在尝试使用堆来解决问题“合并K列表”,它合并了k个已排序的链表并将其作为一个排序列表返回。 Generally I create a min heap to store all of the list node and use predefined function LessThanLinkedList() for the compare. 通常我创建一个min heap来存储所有列表节点,并使用预定义函数LessThanLinkedList()进行比较。 But I found pop_heap() operations in line 62 and 75 never work. 但我发现第62行和第75行中的pop_heap()操作永远不会起作用。 It will not remove the top of the heap, though I used the predefined compare function as a parameter. 虽然我使用预定义的比较函数作为参数,但它不会删除堆的顶部。 The following is my code. 以下是我的代码。 I am using visual studio 2010 as IDE. 我使用visual studio 2010作为IDE。 Anyone knows the reason? 谁知道原因? Thanks a lot for your help! 非常感谢你的帮助!

#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 );
}

pop_heap does not remove elements from the container. pop_heap不会从容器中删除元素。 It can't, since it doesn't even have access to the container, just its elements. 它不能,因为它甚至不能访问容器,只是它的元素。 What it does (assuming of course that [begin, end) forms a valid, non-empty heap) is rearrange the elements such that the first element of the heap moves to be the last element of the range, and leaves [begin, end-1) as a valid heap. 它的作用(当然假设[begin, end)形成一个有效的非空堆)重新排列元素,使得堆的第一个元素移动到该范围的最后一个元素,并离开[begin, end-1)作为有效堆。 If you want to actually remove the element from the container, you just have to erase the last element of the container (eg with a call to pop_back() ) after you call pop_heap . 如果你想从容器中实际删除元素,你只需要在调用pop_heap后擦除容器的最后一个元素(例如调用pop_back() )。

pop_heap( list_heap.begin(), list_heap.end(), LessThanLinkedList );
list_heap.pop_back();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM