PriorityQueue: push, pop and top : Priority Queue « Data Structure « C++






PriorityQueue: push, pop and top

PriorityQueue: push, pop and top
#include <iostream>
#include <string>
#include <queue>
using namespace std;

class Prioritize {
public:
     int operator() ( const pair<string, unsigned int>& p1,
                      const pair<string, unsigned int>& p2 ) {
         return p1.second < p2.second;
     }
};

int main()
{
     priority_queue< pair< string, unsigned int >,
        vector <pair< string, unsigned int > >, Prioritize >   pq;

     pq.push( pair<string, int>( "Event 1", 2) );
     pq.push( pair<string, int>( "Event 2", 10 ) );
     pq.push( pair<string, int>( "Event 3", 1 ) );

     while ( !pq. empty() ) { 
         cout << pq.top().first << endl;
         pq.pop();
     }

     return 0;
}

           
       








Related examples in the same category

1.Demonstrate a priority_queue.Demonstrate a priority_queue.
2.Using a different comparison function: greaterUsing a different comparison function: greater
3.Storing class objects in a priority_queue.Storing class objects in a priority_queue.