Reputation: 529
I was using priority_queue's in a project, and I decided to use a functor for the initialization, but looking on the internet I found this:
std::priority_queue<int, std::vector<int>, std::greater<int> > pq;
I already used it and works, but I'm not really sure why the std::vector<int>
is used there and what underlying containers are and how you manage them (I looked for information, but I don't get the idea).
Upvotes: 0
Views: 1765
Reputation: 98348
std::priority_queue
is not really a container, but a container adaptor, that is, it takes a container and build upon it a different interface.
The underlying container is, naturally, the container that it adapts. By using the C++ template magic you can change the std::vector
into std::deque
and everything will just work.
About what container to use, the same rationale of std::vector
vs std::deque
applies: the std::priority_queue
part does not contain the values, it just presents them in a peculiar way.
Upvotes: 8
Reputation: 308763
This suggests to me that the priority queue data structure is using a vector to hold onto the values. It's being sorted using the greater method so that higher values appear first in the vector, decreasing in order as you go.
It could use another data structure if that made sense, but in this case it chose to use a vector.
Upvotes: 1