【问题标题】:C++ STL pop_heap not workC++ STL pop_heap 不起作用
【发布时间】:2014-12-17 04:26:42
【问题描述】:

我正在尝试使用堆来解决“合并 K 个列表”的问题,该问题正在合并 k 个排序的链表并将其作为一个排序列表返回。通常我创建一个最小堆来存储所有列表节点并使用预定义的函数 LessThanLinkedList() 进行比较。 但是我发现第 62 行和第 75 行中的 pop_heap() 操作永远不会起作用。尽管我使用预定义的比较函数作为参数,但它不会删除堆的顶部。以下是我的代码。我正在使用 Visual Studio 2010 作为 IDE。有人知道原因吗?非常感谢您的帮助!

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

【问题讨论】:

    标签: c++ stl heap


    【解决方案1】:

    pop_heap 不会从容器中移除元素。它不能,因为它甚至不能访问容器,只能访问它的元素。它的作用(当然假设 [begin, end) 形成一个有效的非空堆)是重新排列元素,使堆的第一个元素移动到范围的最后一个元素,并将 [begin, end-1) 保留为有效堆。如果你想真正从容器中移除元素,你只需要在调用pop_heap之后擦除容器的最后一个元素(例如调用pop_back())。

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

    【讨论】:

    • 非常感谢,非常宝贵的答案。好像和原来的stack pop函数不太一样。设计特点我能理解,但会带来一些不一致。
    猜你喜欢
    • 2011-02-18
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2012-03-16
    相关资源
    最近更新 更多