Search code examples
c++priority-queue

priority_queue second parameter :Container object


I know the second parameter of priority_queue is a container(by default it's a vector). But what if I want to use queue or list as the underlying container?

struct compare {
    bool operator()(int&a,int&b) {
        return a > b;
    }
};

int main(int argc, char** argv) {
    std::priority_queue<int, queue<int> , compare >pq();
    return 0;
}

It doesn't work when I use pq.push().
So must priority_queue use vector as a container? Or how can I use queue instead?

I'd appreciate your help very much. Thanx a lot.


Solution

  • From this reference:

    Container - The type of the underlying container to use to store the elements. The container must satisfy the requirements of SequenceContainer and its iterators must satisfy the requirements of RandomAccessIterator. Additionally, it must provide the following functions with the usual semantics:

    • front()
    • push_back()
    • pop_back()

    The standard containers std::vector and std::deque satisfy these requirements.