C++ Priority Queue [closed]
So, spring break begins so this is not quite important, ignore it if you'd like. This code is copied directly from a textbook, but there is an assertion error when it runs. And I'm wondering why?
#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;
}
The error is:
int main(): Assertion `pq.top() == 20' failed.
This code compiles fine for me in g++, but it's making some fatal assumptions.
In line 97 it does an assert(pq.top() == 20).
The problem is pq.top() looks at the front of the vector that is the backing array of the queue.
When you do a pq.push(x), it pushes x to the back of the queue, not the front. So if you fix the code so it pushes the value to the front, it should work fine.
Edit 1: Did you copy and paste this code, or did you type it by hand while reading it? If you did the latter, you may have made an error. I'm looking through the code to see if I can fix it right now.
Edit 2: Actually, did the code for the queue mean to use push_heap() instead of c.push()?
精彩评论