C ++优先级队列

时间:2011-03-21 04:14:28

标签: c++

所以,春假开始所以这不是很重要,如果你愿意,请忽略它。 此代码直接从教科书中复制,但运行时会出现断言错误。而我想知道为什么?

#include <vector>
#include <cassert>
#include <iostream>
using namespace std;

template <class T> class Iterator;

template <class T> 
class Priority_queue {
public:
   Priority_queue() : c() { }
   bool empty() { return c.empty(); }
   unsigned int size() { return c.size(); }
   void push(const T & x) { c.push_back(x); }
   void pop(){ pop_heap (c.begin(), c.end()); }
   T & top() { return c.front(); }
private:
   vector<T> c;
};

template <class Iterator>
void push_heap(Iterator start, Iterator stop)
{
    unsigned int position = (stop - start) - 1;
    unsigned int parent = (position - 1)/2;

    while (position > 0 && start[position] > start[parent]){
    swap (start[position], start[parent]);
    position = parent;
    parent = (position - 1) /2;
    }
}

template <class Iterator>
void make_heap (Iterator start, Iterator stop)
{ 
    unsigned int heapsize = stop - start;
    for (int i = heapsize/2; i >= 0; i--)
    adjust_heap(start, heapsize,i);
}

template <class Iterator>
void sort_heap (Iterator start, Iterator stop)
{
    unsigned int lastPosition = stop - start - 1;
    while (lastPosition > 0) {
        swap (start[0], start [lastPosition]);
    adjust_heap(start, lastPosition, 0);
    lastPosition--;
    }
}

template <class Iterator>
void pop_heap (Iterator start, Iterator stop)
{
    unsigned int lastPosition = (stop - start) - 1;
    swap (start[0], start[lastPosition]);
    adjust_heap(start, lastPosition, 0);
}

template <class Iterator>
void adjust_heap(Iterator start, unsigned heapsize, unsigned position)
{
    while (position < heapsize) {
    unsigned int childpos = position * 2 + 1;
    if (childpos < heapsize) {
        if ((childpos + 1 < heapsize) &&
        start[childpos + 1] > start [childpos])
        childpos++;
    if (start[position] > start[childpos])
        return;
    else
        swap (start[position], start[childpos]);
    }
    position = childpos;
    }
}

template <class Iterator>
void heap_sort(Iterator start, Iterator stop)
{
    make_heap(start,stop);
    sort_heap(start,stop);
}

int main()
{
   Priority_queue<int> pq;

   assert(pq.size() == 0);
   assert(pq.empty());

   pq.push(10);
   assert(pq.top() == 10);

   pq.push(20);
   assert(pq.top() == 20);

   pq.push(30);
   assert(pq.top() == 30);

   pq.push(40);
   assert(pq.top() == 40);

   pq.push(50);
   assert(pq.top() == 50);

   pq.push(5);
   assert(pq.top() == 50);

   pq.pop();
   assert(pq.top() == 40);

   pq.pop();
   assert(pq.top() == 30);

   pq.pop();
   assert(pq.top() == 20);

   pq.pop();
   assert(pq.top() == 10);

   pq.pop();
   assert(pq.top() == 5);

   pq.pop();
   assert(pq.size() == 0);

   Priority_queue<int> pq2;
   pq2.push(30);
   pq2.push(11);
   pq2.push(7);
   pq2.pop();
   assert(pq2.top() == 11);
   pq2.pop();
   assert(pq2.top() == 7);
   pq2.pop();
   assert(pq2.empty());

   cout << "All tests passed." << endl;
}

错误是:

  

int main():断言`pq.top()== 20'   失败。

1 个答案:

答案 0 :(得分:4)

这段代码在g ++中编译好,但是它做了一些致命的假设。

在第97行,它执行一个断言(pq.top()== 20)。

问题是pq.top()查看作为队列支持数组的向量的前面。

当你执行pq.push(x)时,它会将x推送到队列的后面,而不是前面。因此,如果您修改代码以便将值推送到前面,它应该可以正常工作。

编辑1:您是否复制并粘贴了此代码,或者在阅读时是否手动输入了代码?如果你做了后者,你可能犯了一个错误。我正在查看代码,看看我现在是否可以修复它。

编辑2:实际上,队列的代码是否意味着使用push_heap()而不是c.push()?

相关问题