The C++ priority queue is:
template <class T, class Container = vector<T>,
class Compare = less<typename Container::value_type> > class priority_queue;
In this:
T
is the type to be stored in the queue; hopefully this is plain enough.
Container
is the underlying storage within the queue. As the declaration shows, this defaults to the std::vector<T>
, and normally one can ignore it.
Compare' is the method for determining the ordering in the queue. Again, it has a default and can often be ignored.
Compare` is a type supporting a function call which can compare two elements in the queue and determine their ordering.
less
is the default and simply applies the normal <
operator.
When one wants an ordering other than that defined by <
, one defines a type which provides the needed comparison. As you have:
struct comp {
bool operator() (const pii &a, const pii &b) {
return a.second > b.second;
}
};
Note how this takes two pii
and compares using the >
operator; this gives the reverse of the default ordering in the queue.
This comp
type is then specified as the third parameter to the template.
Having specified a third parameter then, with templates as with functions, the second parameter must also be specified even though we only want the same as the default, std::vector<pii>
.
Why the specification of the underlying container? This is the container adaptor
pattern. The idea is that the semantics of Priority Queues do not necessarily imply the underlying way in which the data is to be stored. Also the standard provides a number if data structures specifically focused on storage. Therefore, why not have the priority_queue allow a choice of the underlying storage? This is the adaptor concept.