Reputation: 4265
Every time you want to use a custom compare function for a priority_queue you have to pass it a container too. It seems to me, you should always pass a vector<T>
to it. Now at first I thought this is some kind of redundancy, but it shouldn't be the case. What's the use of passing a container to a priority_queue
and how can I make use of it?
Upvotes: 2
Views: 1269
Reputation: 457
You're simply passing it the underlying container type which, by default, is std::vector<T>
. If you want to retain the default behavior then you're correct in saying you can always pass std::vector<T>
. You can also use std::deque<T>
, or any custom container that exposes the following functions:
But why must you specify a container before you can specify a comparator? Well, it just has to do with how the parameters are defined. container
and compare
are both default parameters, and container
just happens to come first. Because of the way C++ works, you just have to pass all the parameters leading up to compare
before you can pass it, meaning you have to also pass container
. AFAIK it has nothing to do with compare
needing to know anything about container
.
Upvotes: 3
Reputation: 119219
std::priority_queue
is a container adaptor. It doesn't actually know how to store the elements you put in it; it delegates that to a real container such as std::vector
. It's rare that you'll want to use something other than std::vector
as the underlying container, but you could use std::deque
if you want to.
std::vector
is the default, but since this is the second template argument, if you want to use a non-default value for the third template argument, you're forced to specify the second one, just like with function arguments.
Upvotes: 6