Reputation: 53
I tried to allocate memory to the priority_queue
using the constructor
, but getting the below error:
No matching constructor for initialization of 'priority_queue pq(3)'
why this is not working in priority_queue
but working correctly in vector
s?
#include <iostream>
#include <queue>
using namespace std;
int main()
{
priority_queue<int> pqueue(4);
pqueue.push(3);
pqueue.push(5);
pqueue.push(1);
pqueue.push(2);
}
Upvotes: 0
Views: 2147
Reputation: 337
std::priority_queue
doesn't have such constructor, but the below code implements what you want:
std::vector<int> temporary_container(4);
std::priority_queue<int, std::vector<int>> pqueue (comparator, std::move(container));
Also if you want not to change the size of queue
and only reserve memory, you can do it like below:
std::vector<int> temporary_container;
temporary_container.reserve(4);
std::priority_queue<int, std::vector<int>> pqueue (comparator, std::move(container));
Using this ways, you should define your comparator
and pass it to the constructor
.
Upvotes: 2
Reputation: 10972
A std::priority_queue
has a restrictive interface, isn't the same thing as a std::vector
and doesn't have that constructor.
See https://en.cppreference.com/w/cpp/container/priority_queue/priority_queue for constructor summary.
To fix the compile error, you may just do instead:
priority_queue<int> pqueue{};
Upvotes: 0