Is there any way to initialize priority queue with some elements in O(N) complexity? Using the heapify algorithm possibly.
I searched about that problem but couldn't find a solution. Also, I'm aware of make_heap(), but it's another thing and not about priority queues.
Is there any way to initialize priority queue with some elements in O(N) complexity?
Yes; std::priority_queue<...>
has a constructor for this. https://en.cppreference.com/w/cpp/container/priority_queue/priority_queue gives this example:
std::vector<int> vec={3, 1, 4, 1, 5};
std::priority_queue<int> c3(std::less<int>(), vec);
which initializes c3
to a priority queue (max-heap) containing the elements of vec
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.