Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stl priority queue based on lower value first

Tags:

c++

stl

I have a problem with stl priority queue.I want to have the priority queue in the increasing order,which is decreasing by default.Is there any way to do this in priority queue.

And what is the complexity of building stl priority queue.If i use quick sort in an array which takes O(nlgn) is its complexity is similar to using priority queue???

Plz someone ans.Advanced thanx.

like image 341
russell Avatar asked Aug 14 '26 08:08

russell


1 Answers

  1. priority_queue has template parameters that specify the container type and the comparison to use for ordering. By default, the comparison is less<T>; to get the opposite ordering, use priority_queue<T, vector<T>, greater<T>>.

  2. Insertion into a priority queue takes logarithmic time, so building a queue of N items has complexity O(N logN), the same as building and sorting a vector. But once it's built, insertion into the priority queue is still logarithmic, while insertion into a sorted vector is linear.

like image 171
Mike Seymour Avatar answered Aug 16 '26 20:08

Mike Seymour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!