Thread: STL min priority queue?

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    552

    STL min priority queue?

    There is a priority_queue object in stl that gives you a max priority queue but I cant figure out how to get a min queue out of it. I defined my own compare object as im using objects for the elements but there doesnt seem to be anything I can return (from the compare object) to get a min queue. Its either a max queue or some meaningless order. Any ideas?
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but I cant figure out how to get a min queue out of it
    Read it from the other end?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    well its implemented as a heap so reading from the other end wont be in any particular order
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you just have your compare return greater instead of less, won't that work? Note that that's different than not less (which is greater or equal).

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    Why don't you just make the priority inverse ie 1/priority val.

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    This works for me:
    Code:
    struct compare 
    {
      bool operator()(const int& l, const int& r) 
      {
          return l > r;
      }
    };
    
    
    int main()
    {
        priority_queue<int,vector<int>, compare > pq;
        pq.push(3);
        pq.push(5);
        pq.push(1);
        pq.push(8);
        while (!pq.empty())
        {
            cout << pq.top() << endl;
            pq.pop();
        }
        cin.get();
    }
    Quote Originally Posted by dnysveen
    Why don't you just make the priority inverse ie 1/priority val.
    That would be surrendering!
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    38
    Quote Originally Posted by Sang-drax

    That would be surrendering!
    What do you mean it would be surrendering?

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Thanks sang. I actually did try that within the context of my actual program and it didnt work lol. Must have been some weird bug. Thanks for your input guys.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

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. Minimum priority queue STL
    By dpp in forum C++ Programming
    Replies: 7
    Last Post: 01-02-2009, 11:33 AM
  4. c++ stl priority queue
    By dpp in forum C++ Programming
    Replies: 8
    Last Post: 01-01-2009, 11:43 AM
  5. Priority Queue Help
    By cjwenigma in forum C++ Programming
    Replies: 6
    Last Post: 11-15-2007, 12:48 AM