hi...i have made use ofthe priority queue provided by stl....
the default one in max heap.... but i require minheap....how do i do tat....
hi...i have made use ofthe priority queue provided by stl....
the default one in max heap.... but i require minheap....how do i do tat....
You just reverse the comparison predicate for the queue. The default predicate is std::less<T>. If you want it reversed, you need std::greater<T>, i.e. if the priority queue you have now is
then the reverse one isCode:std::priority_queue<int>
Unfortunately, the way the arguments to priority_queue work, you have to specify all arguments if you want to replace the predicate.Code:std::priority_queue< int, std::vector<int>, std::greater<int> >
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
thanks....:
q.pop() pops out the top elemnt...may i know wat fn returns the top elemnt....
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
yup its q.top() ...am i right
can i insert another set of values in the priority queue....
like wat we do in prim's algo... associating key with vertices
both key and vertices stored....
or shud i maintain seprte queue for vertices and its key
You can insert pairs and write a custom comparison predicate that compares keys.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law