Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   C++ STL priority queues (http://www.go4expert.com/forums/cpp-stl-priority-queues-t15695/)

prem 1Jan2009 17:21

C++ STL priority queues
 
hi...
i wanted to implement piority queues using c++ stl....

i want to extract both min and max elemnts (compute with the extracted value as well a s pop them out of queue)
for eg:
std:priority_queue<int> q;
q.push(7);
q.push(2);
i am able to pop the largest since front elemnet is largest...

well i want to pop the smallest elemnts as well..i also want the samallest and largest elemnt to be stored in a temp variable so as to compute sumthng else...

can u guide me on how do i do it here

sharmila 8Jan2009 18:43

Re: C++ STL priority queues
 
I guess we can't delete the smaller value.. but while creating a priority_queue if we create using greater<int> then we can delete small value, but we can't delete higher value..

For the second question can we try somthing like this..
eg:
priority_queue<int> p(q);
int i, j;
i= p.top();
p.pop();
j = p.top();
p.pop();
so that q remains like that and p will be deleted by the end..

I am not sure how much it will work.. so thought to give my thoughts to start the discussion..

If it is not manditory to take a priority_queue we can take vector instead of that..


All times are GMT +5.5. The time now is 11:55.