tags:

views:

75

answers:

3

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.

+1  A: 

Use a different comparator as the 3rd template argument of std::priority_queue.

priority_queue is a container adaptor that works on any sequence you define. The performance of insertion is equal to the std::push_heap operation and takes logarithmic time. So the complexity to sorting after all insertions are done isn't equal. If you insert a fixed amount and work the queue afterwards a vector and a single sort could be more efficient.

pmr
+3  A: 

Use the type

priority_queue<T, vector<T>, greater<T> >
KennyTM
While this solution is (evidently) correct, it does lack: an explanation and an answer to the complexity statement. I know the OP accept rate is pretty terrible and his syntax not that good but still...
Matthieu M.
+5  A: 
  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.

Mike Seymour
+1 for a complete answer
Matthieu M.
Infact i am new in stl.so plz give me an example of priority queue.like i have used as priority_queue<int>Q so what should be the constructor when (priority_queue<T, vector<T>, greater<T>>) is used.i will be greatful if u give the actual code.thanx agin for ur help.
russell
Ok ,thanx everyone who helped me.I got the answer.std::priority_queue<int,std::vector<int, std::allocator<int> >,std::greater<int> > Q;
russell