欣乐

The eagles are coming!

导航

第20章 priority_queue优先队列容器

 

 

 

 

/*

  第20章 priority_queue优先队列容器
   20.1 priority_queue技术原理
   20.2 priority_queue应用基础
   20.3 本章小结

*/


//  第20章 priority_queue优先队列容器
//   20.1 priority_queue技术原理
//   20.2 priority_queue应用基础 ------------------------------------------------------------------------------


//280
#include <queue>
#include <iostream>
int main(void)
{
  using namespace std;
  priority_queue < int > pq;
  pq.push(7);
  pq.push(19);
  pq.push(33);
  pq.push(26);
  pq.push(29);
  while(!pq.empty())
  {
    cout << pq.top() << endl; //打印出33 29 26 19 7
    pq.pop();
  }
  return 0;
}


//281
#include <queue>
#include <iostream>
#define QUEUE_SIZE 50
int main(void)
{
  using namespace std;
  //用双端队列deque做优先队列的底层容器
  priority_queue < int, deque < int >  > pq;
  if(pq.size() < QUEUE_SIZE)
    pq.push(36);
  if(pq.size() < QUEUE_SIZE)
    pq.push(51);
  if(pq.size() < QUEUE_SIZE)
    pq.push(18);
  //元素出队
  while(!pq.empty())
  {
    cout << pq.top() << endl; //打印51 36 18
    pq.pop(); //出队
  }
  return 0;
}



//   20.3 本章小结

 

 

 

 

 

 

TOP

 

posted on 2014-11-20 23:50  欣乐  阅读(146)  评论(0编辑  收藏  举报