Thread: Minimum priority queue STL

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Minimum priority queue STL

    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....

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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
    Code:
    std::priority_queue<int>
    then the reverse one is
    Code:
    std::priority_queue< int, std::vector<int>, std::greater<int> >
    Unfortunately, the way the arguments to priority_queue work, you have to specify all arguments if you want to replace the predicate.
    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

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Smile

    thanks....:

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    q.pop() pops out the top elemnt...may i know wat fn returns the top elemnt....

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    yup its q.top() ...am i right

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    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

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Priority queue STL
    By dpp in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2009, 02:21 AM
  3. c++ stl priority queue
    By dpp in forum C++ Programming
    Replies: 8
    Last Post: 01-01-2009, 11:43 AM
  4. Priority Queue Help
    By cjwenigma in forum C++ Programming
    Replies: 6
    Last Post: 11-15-2007, 12:48 AM
  5. STL min priority queue?
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2006, 11:31 AM