Difference between revisions of "CPP/STL/PriorityQueue"

From ProgrammingExamples
< CPP
Jump to: navigation, search
(Created page with '==PriorityQueue.cpp== <source lang="cpp"> #include <iostream> #include <queue> #include <vector> #include <cstdlib> #include <algorithm> double RandomDouble(); int main(int arg…')
 
 
Line 12: Line 12:
 
{
 
{
 
   std::cout << "TestPriorityQueue" << std::endl << "-------------" << std::endl;
 
   std::cout << "TestPriorityQueue" << std::endl << "-------------" << std::endl;
   std::priority_queue <double> pq;     //pq is a priority queue of integers
+
   std::priority_queue <double> pq;
  
 
   for(unsigned int i = 0; i < 10; i++)
 
   for(unsigned int i = 0; i < 10; i++)

Latest revision as of 12:42, 25 February 2011

PriorityQueue.cpp

#include <iostream>
#include <queue>
#include <vector>
#include <cstdlib>
#include <algorithm>
 
double RandomDouble();
 
int main(int argc, char* argv[])
{
  std::cout << "TestPriorityQueue" << std::endl << "-------------" << std::endl;
  std::priority_queue <double> pq;
 
  for(unsigned int i = 0; i < 10; i++)
  {
    pq.push(RandomDouble());
  }
 
  std::cout<<"pq contains " << pq.size() << " elements.\n";
 
  while (!pq.empty())
  {
    std::cout << pq.top() << std::endl;   //print out the highest priority element
    pq.pop();                   //remove the highest priority element
  }
 
  return 0;
}
 
double RandomDouble()
{
  //produce a random double between 0 and 1
  return drand48();
}