I have a question with priority queue, here are some code:
======================================
//File: tpriorityqueue.cpp
#include <stdlib.h>
#include <time.h>
#include <queue>
#include <vector>
#include <iostream>
struct TestData
{
TestData(int p = 0, int s = 0): priority(p), sequence(s){}
int priority;
int sequence;
};
class HighPriority
{
public:
bool operator()(TestData* left, TestData* right)
{
return (left->priority < right->priority);
}
};
int
main(int, char**)
{
srand(time(0));
std::priority_queue<TestData*, vector<TestData*>, HighPriority> testQueue;
std::cout << "Test Case 1:\n";
for (unsigned int loopInt = 0; loopInt < 10; loopInt++)
{
testQueue.push(new TestData(rand() % 10, loopInt));
}
while ( !testQueue.empty())
{
std::cout << "Priority: " << testQueue.top()->priority;
std::cout << " Sequence: " << testQueue.top()->sequence;
std::cout << endl;
testQueue.pop();
}
std::cout << "Test Case 2:\n";
for (unsigned int loopInt = 0; loopInt < 10; loopInt++)
{
testQueue.push(new TestData(3, loopInt));
}
while ( !testQueue.empty())
{
std::cout << "Priority: " << testQueue.top()->priority;
std::cout << " Sequence: " << testQueue.top()->sequence;
std::cout << endl;
testQueue.pop();
}
}
==================================
The output like this:
==================================
Test Case 1:
Priority: 9 Sequence: 2
Priority: 8 Sequence: 9
Priority: 6 Sequence: 7
Priority: 6 Sequence: 8
Priority: 5 Sequence: 0
Priority: 5 Sequence: 5
Priority: 5 Sequence: 1
Priority: 4 Sequence: 3
Priority: 1 Sequence: 4
Priority: 1 Sequence: 6
Test Case 2:
Priority: 3 Sequence: 0
Priority: 3 Sequence: 2
Priority: 3 Sequence: 6
Priority: 3 Sequence: 9
Priority: 3 Sequence: 8
Priority: 3 Sequence: 5
Priority: 3 Sequence: 7
Priority: 3 Sequence: 4
Priority: 3 Sequence: 1
Priority: 3 Sequence: 3
====================================
My understanding about priority queue is
* The element with highest priority should be popped out first.
* For elements with same priority, they should be popped out chronologically.
According to the output, the first rule is OK. But how about second rule?
Thanks,
Rex Zhang
[code snipped]
> My understanding about priority queue is
> * The element with highest priority should be popped out first.
> * For elements with same priority, they should be popped out
chronologically.
> According to the output, the first rule is OK. But how about second
rule?
>
> Thanks,
> Rex Zhang
I've never heard of the second rule and the standard doesn't seem to
mention it, where are you getting your information?
john
John Harrison wrote:
> > I have a question with priority queue, here are some code:
> > My understanding about priority queue is
> > * The element with highest priority should be popped out first.
> > * For elements with same priority, they should be popped out
> chronologically.
> > According to the output, the first rule is OK. But how about second
> rule?
>
> I've never heard of the second rule and the standard doesn't seem to
> mention it, where are you getting your information?
It seems that he is not the only person which had the same idea. You
don't believe it, if i say that Bjarne also had the same . Actually in
4-th printing , bjarne said that 'Elements with equal priority come to
the head of the queue in the order in which they were inserted. That is,
for elements of equal priority, a priority_queue is simply a queue.'
But then there was an errata regarding that mistake. The corrected one
was 'The order in which elements with equal priority come to the head of
the queue is not defined.'
Check out : http://www.research.att.com/~bs/3rd_printing5.html for
conformation.
--
Nithyanand.