Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain Comparison type of priority_queue?

Most similar containers have member types like key_compare or value_compare but there none for priority_queue.

Is that because priority_queue is an adaptor? Or is this in the standard by mistake?

like image 774
Isaac Pascual Avatar asked Apr 28 '16 09:04

Isaac Pascual


People also ask

Can we use comparator function in priority queue C++?

You may have often come to a situation when you need to use a priority queue, but your datatype is something else that can't be compared by default (using '<' operator what is used by default). In such cases, we need to declare our comparator function. We can use the lambda function for that.

How is STL priority queue implemented in C++?

C++ Software Engineering The priority queue in the STL of C++ is a dynamically resizing container to implement the special case of priority queye under the queue data structure. It is a sequential linear container. The top element of the priority queue is the greatest with all elements arranged in non-increasing order.

How do you clear the priority queue in CPP?

The solution that worked best for me is just to use "new" and "delete" and use a ptr to priority_queue.


1 Answers

Yes, this is weird indeed and seems to be an oversight in the C++ standard. The standard declares priority_queue as:

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

And while it specifies the following public members:

typedef typename Container::value_type value_type;
typedef typename Container::reference reference;
typedef typename Container::const_reference const_reference;
typedef typename Container::size_type size_type;
typedef Container container_type;

it for some reason omits a typedef Compare value_compare; declaration. A C++ Standard Library issue for this has been submitted to the Library Working Group (LWG) by SO user Columbo.

Workarounds

However, you can use the following to work around the issue.

In C++11 and later

#include <queue>

template <typename> struct GetCompare;

template <typename T, typename Container, typename Compare>
struct GetCompare<std::priority_queue<T, Container, Compare> >
{ using type = Compare; };

template <typename T>
using GetCompare_t = typename GetCompare<T>::type;

For example:

#include <type_traits>

static_assert(
    std::is_same<
        GetCompare_t<std::priority_queue<int> >,
        std::less<int>
    >::value, "");

In C++03 and earlier

#include <queue>

template <typename> struct GetCompare;

template <typename T, typename Container, typename Compare>
struct GetCompare<std::priority_queue<T, Container, Compare> >
{ typedef Compare type; };

For example:

int main() {
    return typename GetCompare<std::priority_queue<int> >::type()(42, 0);
}
like image 170
jotik Avatar answered Nov 15 '22 12:11

jotik