Search code examples
c++stlpriority-queue

Declaration of Priority Queue using greater and vector


Can anybody please explain what is the meaning of this declaration:

typedef pair<long long, int> PII;
priority_queue<PII, vector<PII>, greater<PII> > Q;

Is it to be treated as a priority queue of pair of long long and int or something else? Can please someone also explain what is greater<PII> here?


Solution

  • This declares a std::priority_queue containing std::pair<long long, int> instances where a std::vector<std::pair<long long, int>> is the underlying container of the priority queue (as it is a container adaptor).

    The std::greater<std::pair<long long, int>> is used as the Comparator function object for the queue, checking whether the LHS pair is greater than the RHS pair. See below for a reference:

    http://en.cppreference.com/w/cpp/utility/functional/greater