STL priority queue

Status
Not open for further replies.
I have a class thats currently working decently in an STL list. I'd like to now create a priority queue with this class- but instead of highest priority, i want to have the object with the lowest value in a certain field be at the head of the queue.

how would I go about doing that? What methods/overloads do i need to add to my class to support the ordering?

- dan

edit: i'm reading the docs, but part of my problem is that i'm not 100% sure of the various syntax and terminology involved with templates and containers.

edit 2: for example, what is the difference between a container and an adapter? it sounds to me as if an adapter would let you put your own class into a container, if the class doesn't properly support what the container requires. is this sorta right?
 
The priority queue provided by STL allows you to supply your own comparison operator so you can customize the queue behavior. By default it will use less than, thereby sorting your sequence into decending order. To order your queue in ascending order you could use the greater than function object, std::greater<>.
Code:
#include <queue>
#include <vector>
#include <functional>
#include <iostream>

int main()
{
    std::priority_queue<int, std::vector<int>, std::greater<int> > my_queue;
    my_queue.push( 10 );
    my_queue.push( 90 );
    my_queue.push( 5 );
    while( !my_queue.empty() )
    {
        std::cout << my_queue.top() << std::endl;
        my_queue.pop();
    }

    // Prints: 5, 10, 90
    return 0;
}

Be sure that if you are putting objects in your queue that you overload the appropriate comparison operator in your class. Of course, this is not the only means of suppling the queue with an ordering function, but it tends to be the simplest. Particularly wrt less than and greater than.

STL components can be divided into five categories, one of which is adaptors. Adaptors allow us to modify the behavior or interface of another component. std::priority_queue and std::stack are examples of container adaptors. They provide a means for adapting any container and making it behave like a priority queue or a stack respectively as long as the container meets the requirements for efficient implementation of a stack or queue. Constant time removal from the end of the sequence for example. There are many other examples of adapters in STL, std::back_inserter is an iterator adaptor and std::bind2nd is a function adaptor.
 
ok, i *think* i understand that last part. the other first part helped me immensely. (or rather, if this thing compiles in about 20 minutes, it'll have helped me :))

a question:
if i have a priority_queue, can i cycle through each entry? i notice there aren't any iterator methods like begin(), so i can't cycle through like my list (to run a method on each of the items in the p_queue).

is there a way to do that, short of front().run(), copying that element to a new p_queue, and pop()ing the item off the old p_queue? that seems awfully inefficient. i don't know what the O() notation for that is, but i have a feeling it would involve a lot of n's and be really complex :))

until i figure that out, i'm doing a cheapy-way around it.

edit: ok, my attempt to use priority_queues resulted in compiletime errors that i've never even HEARD of :)

Code:
c:\program files\microsoft visual studio\vc98\include\functional(80) : error C2784: 'bool __cdecl std::operator >(const class std::list<_Ty,_A> &,const class std::list<_Ty,_A> &)' : could not deduce template argument for 'const class std::list<_Ty,_
A> &' from 'const class Pcb'
        c:\program files\microsoft visual studio\vc98\include\functional(80) : while compiling class-template member function 'bool __thiscall std::greater<class Pcb>::operator ()(const class Pcb &,const class Pcb &) const'
c:\program files\microsoft visual studio\vc98\include\functional(80) : error C2784: 'bool __cdecl std::operator >(const class std::vector<_Ty,_A> &,const class std::vector<_Ty,_A> &)' : could not deduce template argument for 'const class std::vector
<_Ty,_A> &' from 'const class Pcb'
        c:\program files\microsoft visual studio\vc98\include\functional(80) : while compiling class-template member function 'bool __thiscall std::greater<class Pcb>::operator ()(const class Pcb &,const class Pcb &) const'
c:\program files\microsoft visual studio\vc98\include\functional(80) : error C2784: 'bool __cdecl std::operator >(const class std::deque<_Ty,_A> &,const class std::deque<_Ty,_A> &)' : could not deduce template argument for 'const class std::deque<_T
y,_A> &' from 'const class Pcb'
        c:\program files\microsoft visual studio\vc98\include\functional(80) : while compiling class-template member function 'bool __thiscall std::greater<class Pcb>::operator ()(const class Pcb &,const class Pcb &) const'
c:\program files\microsoft visual studio\vc98\include\functional(80) : error C2784: 'bool __cdecl std::operator >(const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce te
mplate argument for 'const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &' from 'const class Pcb'
        c:\program files\microsoft visual studio\vc98\include\functional(80) : while compiling class-template member function 'bool __thiscall std::greater<class Pcb>::operator ()(const class Pcb &,const class Pcb &) const'
c:\program files\microsoft visual studio\vc98\include\functional(80) : error C2784: 'bool __cdecl std::operator >(const struct std::pair<_T1,_T2> &,const struct std::pair<_T1,_T2> &)' : could not deduce template argument for 'const struct std::pair<
_T1,_T2> &' from 'const class Pcb'
        c:\program files\microsoft visual studio\vc98\include\functional(80) : while compiling class-template member function 'bool __thiscall std::greater<class Pcb>::operator ()(const class Pcb &,const class Pcb &) const'
c:\program files\microsoft visual studio\vc98\include\functional(80) : error C2676: binary '>' : 'const class Pcb' does not define this operator or a conversion to a type acceptable to the predefined operator
        c:\program files\microsoft visual studio\vc98\include\functional(80) : while compiling class-template member function 'bool __thiscall std::greater<class Pcb>::operator ()(const class Pcb &,const class Pcb &) const'
Error executing cl.exe.
Creating browse info file...

scheduler2.exe - 6 error(s), 0 warning(s)
 
The priority_queue adaptor provided by STL doesn't. The queue is strictly immutable. However, it is possible to roll your own with very little effort. How you implement your queue is dependent on whether or not you want to iterate over the queue in order or not.

If order doesn't matter then I would use a heap implementation like the one I linked. If order does matter then I would use a sorted random access container instead (an array implementation). To insert elements in this case I would use either lower_bound or upper_bound to find the correct insertion position depending on whether or not the relative order of the elements is significant.
 
Status
Not open for further replies.