C++ What's the best way to implement a Priority Queue with varying priority functions?

knowads
:

The accepted answer I've seen for swapping out a priority queue comparator is to overload the operator in a new compare class.

class Foo
{

};

class Compare
{
public:
    bool operator() (Foo, Foo)
    {
        return true;
    }
};

int main()
{
    std::priority_queue<Foo, std::vector<Foo>, Compare> pq;
    return 0;
}

However, I want to implement several (10+) different compare functions for queue and choose one at run time when pq is created in main(). Do I have to make 10 different compare classes or is there an easier way to do this?

artm
:

Do I have to make 10 different compare classes or is there an easier way to do this?

You don't have to. The priority_queue requires that the comparator taking a Foo and return bool - with the default one is std::less

template<
    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>
> class priority_queue;

In your case, you may use a lambda, or a pointer to function for that purpose. For example,

using cmp1 = bool(*)(const Foo&, const Foo&);
bool FooCmp1(const Foo& f1, const Foo& f2)
{
     // do real comparison..
     return true;
}

priority_queue<Foo, std::vector<Foo>, cmp1> pq(FooCmp1);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is there a simple way to implement a fast priority queue in Haskell?

From Dev

Trying to Implement a priority queue in a different way

From Dev

Is there a way to check if a priority exists in a priority queue in Python?

From Dev

Implement a priority queue using stack

From Dev

Implement a priority queue using stack

From Dev

How to implement regular queue using priority queue?

From Dev

Increase priority in priority queue

From Dev

What's the use of passing a container to priority_queue

From Dev

Priority queue and Prim's Algorithm

From Dev

Priority Queue Objective-C++?

From Dev

Implementing priority queue in C++

From Dev

C++ priority_queue

From Dev

Priority Queue Objective-C++?

From Dev

Implementing priority queue in C++

From Dev

Implement a priority queue using only ONE stack

From Dev

How to implement a priority queue using two queues

From Dev

Use a linked list to implement a Priority Queue

From Dev

How to implement a stack using a priority queue?

From Dev

How to implement a priority queue using two queues

From Dev

How to implement priority queue with unordered linkedlist

From Dev

Best Data Structure for Priority Queue implementation

From Dev

How can I implement Priority Queue without using any built-in functions?

From Dev

What is the angularfire way of dealing with $priority?

From Dev

About C++ priority_queue what does vector<int> do in priority_queue<int, vector<int>, greater<int> > pq?

From Dev

C++ template functions priority

From Dev

Priority queue with two priority values

From Dev

Python: priority queue with time as priority

From Dev

how to set priority in priority queue

From Dev

What are the template parameters of std::priority_queue?

Related Related

HotTag

Archive