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?
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.
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.
The solution that worked best for me is just to use "new" and "delete" and use a ptr to priority_queue.
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.
However, you can use the following to work around the issue.
#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, "");
#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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With