1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ STL priority queues

Discussion in 'C++' started by prem, Jan 1, 2009.

  1. prem

    prem New Member

    Joined:
    Jan 1, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    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..
     

Share This Page