Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

priority queue

99 views
Skip to first unread message

Rex Zhang

unread,
Jul 18, 2001, 2:34:00 PM7/18/01
to
Hi, everyone:

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

John Harrison

unread,
Jul 18, 2001, 2:53:48 PM7/18/01
to

"Rex Zhang" <qi...@zhang.net> wrote in message
news:e5622a7e.01071...@posting.google.com...

> Hi, everyone:
>
> I have a question with priority queue, here are some code:
>

[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

Nithyanandham

unread,
Jul 18, 2001, 3:48:52 PM7/18/01
to

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.

elizun...@gmail.com

unread,
Nov 27, 2014, 12:08:18 PM11/27/14
to
Hi, i have a problem with my priority queue i am trying to create a priority queue of a class called duty with three elements but it produces an error. the sentence is :

the .h file is
///////////////////////////////////////////////////////////////////////////
#ifndef DUTY_H
#define DUTY_H


class duty
{
public:
duty();
virtual ~duty();

double costo_reducido;
double costo_real;
unsigned id;
bool operator ==( const duty &d2) ;
bool operator <( const duty &d2) ;
bool operator >( const duty &d2) ;
};

#endif //

////////////////////////////////////////////////////////////////////////////
the .cpp file is
//////////////////////////////////////////////////////////////////////////

#include "duty.h"

duty::duty()
{
id=0.1;
costo_reducido=0;
costo_real=0;
}

duty::~duty()
{
//dtor
}
bool duty::operator == (const duty &d1)
{
return this->costo_reducido == d1.costo_reducido;
}
bool duty::operator < (const duty &d1)
{
return this->costo_reducido < d1.costo_reducido;
}
bool duty::operator > (const duty &d1)
{
return this->costo_reducido > d1.costo_reducido;
}

////////////////////////////////////////////////////////////////////
and the main.cpp file is

#include <iostream>
#include <queue>
#include "duty.h"
using namespace std;

int main()
{
priority_queue <duty> my;
duty a;
a.costo_real=4;
a.costo_reducido=10;
a.id=0;

duty b;
b.costo_real=5;
b.costo_reducido=8;
b.id=1;

duty c;
c.costo_real=6;
c.costo_reducido=100;
c.id=2;

duty d;
d.costo_real=7;
d.costo_reducido=9;
d.id=3;

my.push(a);
my.push(b);
my.push(c);
my.push(d);

//ordena de mayor a menor


while (!my.empty())
{
duty o;
o=my.top();
my.pop();
std::cout << ' ' << o.id<<endl;

}

return 0;
}

I dont know what i am doing bad. Can anyone help me??

Barry Schwarz

unread,
Nov 27, 2014, 12:24:40 PM11/27/14
to
On Thu, 27 Nov 2014 09:08:06 -0800 (PST), elizun...@gmail.com
wrote:

>Hi, i have a problem with my priority queue i am trying to create a priority queue of a class called duty with three elements but it produces an error. the sentence is :

It seems you have forgotten to tell us what the error is.

>the .h file is
>///////////////////////////////////////////////////////////////////////////
>#ifndef DUTY_H
>#define DUTY_H
>
>
>class duty
>{
> public:
> duty();
> virtual ~duty();
>
>double costo_reducido;
>double costo_real;
>unsigned id;
> bool operator ==( const duty &d2) ;
> bool operator <( const duty &d2) ;
> bool operator >( const duty &d2) ;
>};

<snip>

>duty::duty()
>{
> id=0.1;

Since id is an unsigned int, why are you assigning it a double?

> costo_reducido=0;
> costo_real=0;
>}

Here is a hint: none of the overloaded operators you define modify
either operand.

--
Remove del for email

elizun...@gmail.com

unread,
Nov 27, 2014, 12:38:27 PM11/27/14
to
Yeah you are right , but the id is not the problem the message is

"instantiated from here" where i declared my priority queue.

The question is how can a create a priority queue of a class because it seems what i do doesnt work.

Vincenzo Mercuri

unread,
Nov 27, 2014, 1:16:18 PM11/27/14
to
Il 27/11/2014 18:08, elizun...@gmail.com ha scritto:
...
>
> I dont know what i am doing bad. Can anyone help me??
>

Other than what you've already been told, be aware that objects
in std::priority_queue are stored as const, and you can't call
a non-const member function on a const object.

--
Vincenzo Mercuri

Mr Flibble

unread,
Nov 27, 2014, 2:34:57 PM11/27/14
to
Nonsense.

/Flibble


Martijn Lievaart

unread,
Nov 27, 2014, 3:00:11 PM11/27/14
to
Well, how do you manipulate an object in a priority queue?

M4

Bix

unread,
Nov 27, 2014, 3:56:40 PM11/27/14
to
You don't. You pop, manipulate and push.

I think that the point is that they must be const and
operators are declared non const so I guess this is one of the problem.

If you think about it the priority_queue is defined by
template<
class T,
class Container = std::vector<T>,
class Compare = std::less<typename Container::value_type>
> class priority_queue;

So it's going to use the operator< but because it's declared as non
const calling the operator< potentially change the ordering :)

cheers
f.

Mr Flibble

unread,
Nov 27, 2014, 4:31:58 PM11/27/14
to
That might be the case but the *objects* inside the priority queue are
not const objects like the OP claimed.

/Flibble

Barry Schwarz

unread,
Nov 27, 2014, 5:55:25 PM11/27/14
to
On Thu, 27 Nov 2014 09:38:16 -0800 (PST), elizun...@gmail.com
wrote:

>Yeah you are right , but the id is not the problem the message is
>
>"instantiated from here" where i declared my priority queue.

This is a completely different message than my system generates.

>The question is how can a create a priority queue of a class because it seems what i do doesnt work.

That is most likely not the question. The more probable question is
how to define the class properly so a priority queue can be
implemented.

You need to provide the complete error message and identify the line
of code the compiler is referring to.

Go back and look at the hint I gave you in my last message.

Vincenzo Mercuri

unread,
Nov 27, 2014, 8:09:46 PM11/27/14
to
And how do you call objects that you cannot modify?

--
Vincenzo Mercuri

Mr Flibble

unread,
Nov 27, 2014, 8:30:33 PM11/27/14
to
But you can modify them.

const int i = 42; /* (1) a const object */
int j = 43; /* (2) a non-const object */
const int& r = j; /* (3) a reference-to-const to a non-const object */

We are talking about case (3) here: the underlying container of
priority_queue is std::vector by default and it is not possible to store
const objects in a std::vector.

Theoretically you can use const_cast<> on the priority_queue top()
element to "remove const" and modify it and you should not run into any
problems as long as that change doesn't affect ordering.

/Flibble

Vincenzo Mercuri

unread,
Nov 27, 2014, 9:24:09 PM11/27/14
to
That's where our misunderstanding lies. With "stored as const" I mean
that they can only be accessed by means of reference-to-const, also
because it wouldn't make much sense to "store const objects", for the
same reason why a "vector<const int>" is meaningless. In our case,
std::priority_queue uses std::less<> by default (as its Compare type),
which provides a std::less::operator() declared as:

bool operator()( const T& lhs, const T& rhs ) const;

that in turn requires T (OP's "duty"..) to define operator< which *must*
be const because it's called by a const member function. [If the OP
had to define his/her type for a container like vector, operator<
wouldn't be required to be const anymore].

>
> Theoretically you can use const_cast<> on the priority_queue top()
> element to "remove const" and modify it and you should not run into any
> problems as long as that change doesn't affect ordering.

I understand that but that was not my point: a reference-to-const
is a reference-to-non-const via const_cast<> as much as an int is
a double via static_cast<>.

--
Vincenzo Mercuri

Mr Flibble

unread,
Nov 28, 2014, 8:54:00 AM11/28/14
to
On 28/11/2014 02:24, Vincenzo Mercuri wrote:
[snip]
>
> I understand that but that was not my point: a reference-to-const
> is a reference-to-non-const via const_cast<> as much as an int is
> a double via static_cast<>.

Wrong. The double would be a different object to the int when using
static_cast<> whilst the result of the const_cast<> returns the *same*
object.

/Flibble

blackball

unread,
Nov 29, 2014, 10:24:44 PM11/29/14
to
Two different questions in one thread ? I have the same confusion with the first author.

Öö Tiib

unread,
Dec 1, 2014, 4:56:18 AM12/1/14
to
On Friday, 28 November 2014 04:24:09 UTC+2, Vincenzo Mercuri wrote:
> In our case,
> std::priority_queue uses std::less<> by default (as its Compare type),
> which provides a std::less::operator() declared as:
>
> bool operator()( const T& lhs, const T& rhs ) const;
>
> that in turn requires T (OP's "duty"..) to define operator< which *must*
> be const because it's called by a const member function.

Why it matters if 'const' or not 'const' member 'operator()' of 'std::less<>' is called? 'std::less<>' is anyway stateless unless
you have yourself specialized it as stateful.

All comparators take the arguments that are compared as immutable (or
by value) because comparison that mutates its arguments is unexpected
and confusing even to its author.


Vincenzo Mercuri

unread,
Dec 3, 2014, 7:09:42 AM12/3/14
to
Il 01/12/2014 10:56, Öö Tiib ha scritto:
> On Friday, 28 November 2014 04:24:09 UTC+2, Vincenzo Mercuri wrote:
..
> Why it matters if 'const' or not 'const' member 'operator()' of 'std::less<>' is called? 'std::less<>' is anyway stateless unless
> you have yourself specialized it as stateful.
>
..

I'm not sure why I pointed out the 'constness' of member operator().
That is actually irrelevant. I apologize, you are right.


--
Vincenzo Mercuri

Vincenzo Mercuri

unread,
Dec 3, 2014, 7:27:45 AM12/3/14
to
Il 28/11/2014 14:53, Mr Flibble ha scritto:
>
> The double would be a different object to the int when using
> static_cast<> whilst the result of the const_cast<> returns the *same*
> object.

I didn't say it doesn't. I said that 'const T& obj' and 'T& obj' are not
the same thing. Forcing something to be something else is another story.

--
Vincenzo Mercuri

Mr Flibble

unread,
Dec 3, 2014, 12:46:25 PM12/3/14
to
They could both refer to the same object though and if the object is
non-const it is perfectly valid to cast away const from the reference.
This is totally different to your static_cast example.

/Flibble


0 new messages