priority queue in STL

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
Solaris
Learning poster
Posts: 99
Joined: Sun Apr 06, 2003 5:53 am
Location: Dhaka, Bangladesh
Contact:

priority queue in STL

Post by Solaris » Mon Oct 31, 2005 8:53 pm

I have been trying to use priority queue as a min-heap for integers.
I have used something like

Code: Select all

struct RevInt
{
    int value;

    bool operator<(const RevInt &b) const
    {
         return b.value < value;
    }
}

priority_queue<RevInt> pq;


But I was wondering how to use the constructor that overloads the default Compare function for integer. What I mean to say is I want to use something like

Code: Select all

priority_queue<int> pq;
how can I do that ??
Where's the "Any" key?

Bj
New poster
Posts: 24
Joined: Mon Oct 17, 2005 1:39 am
Location: Sweden

Post by Bj » Mon Oct 31, 2005 11:17 pm

Try this:

Code: Select all

// normal priority queue
priority_queue<int> pq1;

// reversed
priority_queue< int, vector<int>, greater<int> > pq2;
The default priority queue looks exactly like the "reversed" one above but uses std::less instead of std::greater as the comparision routine.

Solaris
Learning poster
Posts: 99
Joined: Sun Apr 06, 2003 5:53 am
Location: Dhaka, Bangladesh
Contact:

Post by Solaris » Tue Nov 01, 2005 6:07 pm

Thank you Bj
Where's the "Any" key?

Post Reply

Return to “C++”