473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Priority Queues in Visual Studio 6.0

Are priority queues implemented in the STL in Visual Studio 6? If no, which
is the easiest way to simulate them? Multisets?
BTW: The container contains pointers to instances of a class. The ordering
of the elements must follow the value of the class-instances not the value
of the pointers. Furthermore, elements are inserted throughout execution of
the program so I need a insert function which keeps the elements sorted; a
sort function which sorts the whole container after every insertion is not
sufficient (and surely not efficient).

Regards,
Matthias


Jul 22 '05 #1
6 2461
"Der Andere" <ma**************@gmx.net> wrote in message
news:c9*************@news.t-online.com...
Are priority queues implemented in the STL in Visual Studio 6? If no, which is the easiest way to simulate them? Multisets?
BTW: The container contains pointers to instances of a class. The ordering
of the elements must follow the value of the class-instances not the value
of the pointers. Furthermore, elements are inserted throughout execution of the program so I need a insert function which keeps the elements sorted; a
sort function which sorts the whole container after every insertion is not
sufficient (and surely not efficient).


See template class priority_queue in the header <queue>. You'll
have to supply an ordering predicate that takes two pointers
and returns true when one of the designated objects is ordered
before the other, but that shouldn't be hard. The queue stores
the pointers in a random-access container, which it maintains
as a heap. Pushing new items on the heap and popping the highest
priority items off the heap each take log(N) percolations, where
N is the total size of the heap (queue). Hard to do better.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #2
> > Are priority queues implemented in the STL in Visual Studio 6? If no,
which
is the easiest way to simulate them? Multisets?
BTW: The container contains pointers to instances of a class. The ordering of the elements must follow the value of the class-instances not the value of the pointers. Furthermore, elements are inserted throughout execution

of
the program so I need a insert function which keeps the elements sorted; a sort function which sorts the whole container after every insertion is not sufficient (and surely not efficient).


See template class priority_queue in the header <queue>. You'll
have to supply an ordering predicate that takes two pointers
and returns true when one of the designated objects is ordered
before the other, but that shouldn't be hard. The queue stores
the pointers in a random-access container, which it maintains
as a heap. Pushing new items on the heap and popping the highest
priority items off the heap each take log(N) percolations, where
N is the total size of the heap (queue). Hard to do better.


Written by you. Nice to have people around who know quite well what they are
talking about ;-)))

Cheers,
Matthias
Jul 22 '05 #3
Der Andere wrote:
Written by you. Nice to have people around who know quite well what they
are talking about ;-)))


Well, a d-heap is not that hard to write anyway... It is more interesting
to write heaps with modifiable priority. Although a d-heap operators quite
good there, too, the theoretical best implementation is a Fibonacci-heap
which is a little bit harder to implement.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #4
> > Are priority queues implemented in the STL in Visual Studio 6? If no,
which
is the easiest way to simulate them? Multisets?
BTW: The container contains pointers to instances of a class. The ordering of the elements must follow the value of the class-instances not the value of the pointers. Furthermore, elements are inserted throughout execution of
the program so I need a insert function which keeps the elements sorted; a sort function which sorts the whole container after every insertion is not sufficient (and surely not efficient).


See template class priority_queue in the header <queue>. You'll
have to supply an ordering predicate that takes two pointers
and returns true when one of the designated objects is ordered
before the other, but that shouldn't be hard.


The following sample program does not compile but I have no idea what to do
about it. Might be a beginner's fault. If I substitute pv(comp()) with
pv(std::less<Vertex*>()) the compiler gives no error, but I strongly suppose
less() would order the elements according to the address value of the
pointer.

#include <queue>

struct Vertex
{
int a;
};

bool comp (Vertex* v1, Vertex* v2)
{
return v1->a < v2->a;
}

int main()
{
std::priority_queue<Vertex*> pv(comp());
return 0;
}
Regards,
Matthias

the pointers in a random-access container, which it maintains
as a heap. Pushing new items on the heap and popping the highest
priority items off the heap each take log(N) percolations, where
N is the total size of the heap (queue). Hard to do better.



Jul 22 '05 #5
> > Written by you. Nice to have people around who know quite well what they
are talking about ;-)))


Well, a d-heap is not that hard to write anyway... It is more interesting
to write heaps with modifiable priority. Although a d-heap operators quite
good there, too, the theoretical best implementation is a Fibonacci-heap
which is a little bit harder to implement.


Yes, had a look on Fibonacci-heaps and decided not to take another before
next year ;-)

Regards,
Matthias
Jul 22 '05 #6
Der Andere wrote:
bool comp (Vertex* v1, Vertex* v2)
{
return v1->a < v2->a;
}

int main()
{
std::priority_queue<Vertex*> pv(comp());


There are two things which are wrong with the above code:
- The type of the comparator is part of the priority queue's type.
That is, the type in the above line would read something like
this:

std::priority_queue<Vertex*, std::vector<Vertex*>,
bool(*)(Vertex*, Vertex*)>

- 'comp' is a function, not a type. To pass 'comp' to a function
like the constructor you just use 'comp':

pv(comp)

Of course, 'std::less<Vertex*>' would indeed compare the pointer
values.

Since you apparently want to use your priority queue with a graph,
you might want to use a priority queue allowing changing a nodes
priority. 'std::priority_queue' does not allow this. I have a
made a collection of priority queues publically available, some of
which allow this (e.g. the Fibonacci-heap and a specialized d-heap
implementation). These can be downloaded from
<http://www.dietmar-kuehl.de/heaps.tar.gz>.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: wtfhits | last post by:
I'm writing my own task manager as I often change process priorities but have gotten myself into trouble on several occasions. I have discovered by keeping csrss.exe and explorer at the equal highest...
38
by: Aaron W. LaFramboise | last post by:
Hello, I understand that an easy way to make the standard std::priority_queue stable is by including an integer stamp with each node that is incremented each time a new node is pushed into the...
1
by: Der Andere | last post by:
I posted this reaction in a thread quite far below so I fear people won't read it. I got stuck now for quite a while and just don't know what to try else. Thanks, Matthias > > Are priority...
1
by: Jim Strathmeyer | last post by:
So, I've been looking for a wrapper class for STL priority queues, and haven't been able to find one. (Websearching for info about the STL sure shows how much it's changed over the past few years.)...
5
by: Dan H. | last post by:
Hello, I have implemented a C# priority queue using an ArrayList. The objects being inserted into the priority queue are being sorted by 2 fields, Time (ulong) and Priority (0-100). When I...
8
by: vidishasharma | last post by:
Can somebody suggest good URL which contains code for merging of 2 or more priority queues in c#.
5
by: kidfiction | last post by:
Hello again, I was wondering if you could make priority queues with defined compare functions out of structs rather than objects. Currently I have this (with some things omitted: bool...
0
by: DCC700 | last post by:
Here are a couple of errors I have found in the event log system.typeinitialization NIL system.messaging.messagequeue NIL These occur when trying to send a message through ms queues to a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.