首页 > 代码库 > 优先队列

优先队列

 1 #include <iostream> 2 #include <vector> 3 #include <queue> 4 #include <functional> 5 #include <string> 6 using namespace std; 7  8 // Empty the priority queue and print its contents. 9 template <typename PriorityQueue>10 void dumpContents( const string & msg, PriorityQueue & pq )11 {12     cout << msg << ":" << endl;13     while( !pq.empty( ) )14     {15         cout << pq.top( ) << endl;16         pq.pop( );17     }18 }19 20 // Do some inserts and removes (done in dumpContents).21 int main( )22 {23     priority_queue<int>                          maxPQ;24     priority_queue<int,vector<int>,greater<int>> minPQ;25 26     minPQ.push( 4 ); minPQ.push( 3 ); minPQ.push( 5 );27     maxPQ.push( 4 ); maxPQ.push( 3 ); maxPQ.push( 5 );28 29     dumpContents( "minPQ", minPQ );30     dumpContents( "maxPQ", maxPQ );31 32     return 0;33 }