首页 > 代码库 > Priority Queues

Priority Queues

由小到大

#include <iostream>

#include <queue>

#include <vector>

using namespace std;

int main()

{        

       priority_queue < int,    vector<int> , greater<int>  >   q;     

                          q.push(3);      

                          q.push(2);

                          q.push(1);

                         q.push(7);

 while ( !q.empty())    

     {  

              cout<<q.top()<<endl;            

                      q.pop();    

          }    

return 0;

}

 

 

 

//由大到小

#include <iostream>

#include <queue>

using namespace std;

int main()

{    

priority_queue <int>  q;    

              q.push(3);   

             q.push(2);

             q.push(1);

            q.push(7);

while ( !q.empty())    

    {        

                  cout<<q.top()<<endl;     

                           q.pop();    

    }    

return 0;

}

 

 

 

 

************************************************************************************************

 

 

#include <iostream>
#include <queue>
using namespace std;
int main()
{
    priority_queue<int> q;
    for(int i = 1;i <= 5;i++)
    {
        q.push(i);
    }
    for(int i = 0;i < 5;i++)
    {
        cout<<q.top()<<endl;
        q.pop();
    }
    return 0;
}

 

 

 

************************************************************************************************************************

 

 

#include <iostream>

#include <queue>

using namespace std;

int main()

{    

                    priority_queue<int> q;   

                int i;    

   while (cin>>i)

          q.push(i);

    cout<<q.top()<<endl;

 

              q.pop();

    return 0;

}