Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2783175
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: C/C++

2012-08-07 14:33:13

 
1、基本类型

点击(此处)折叠或打开

  1. #pragma warning(disable:4786)

  2. #include <iostream>
  3. #include<queue>
  4. #include<vector>
  5. #include<stdio.h>
  6. using namespace std;

  7. struct myComp
  8. {
  9.     //STL中默认的是最大优先队列
  10.     bool operator () (const int &a,const int &b)
  11.     {
  12.         return a>b;//最小堆
  13.     }
  14. };

  15. int main()
  16. {
  17.     priority_queue<int,vector<int>,myComp>pq;
  18.     pq.push(1);
  19.     pq.push(2);
  20.     pq.push(3);
  21.     pq.push(9);
  22.     while(pq.empty()!=true)
  23.     {
  24.         cout<<pq.top()<<" ";
  25.         pq.pop();
  26.     }
  27.     cout<<endl;
  28.     return 0;
  29. }
  30. /*
  31. 1 2 3 9
  32. Press any key to continue

  33. */
2、结构体
//小堆

点击(此处)折叠或打开

  1. struct node
  2. {
  3.     int time,Q_num;
  4. };
  5. struct cmp
  6. {
  7.     bool operator()(const node& a,const node& b)
  8.     {
  9.         if(a.time!=b.time) return a.time>b.time;
  10.         else return a.Q_num>b.Q_num;
  11.     }
  12. };
  13. priority_queue<node,vector<node>,cmp> task;


点击(此处)折叠或打开

  1. struct Stone{
  2.  int x; //石头的初始地
  3.  int y; //石头能扔的最远距离
  4.  };
  5.  booloperator<( Stone a, Stone b )
  6.  { //重载小于,按照结构体中x小的在队顶,如果x一样,则按照y的最小的在//

  7. 队顶
  8.  if( a.x== b.x ) return a.y > b.y;
  9.  return a.x > b.x;
  10.  }

  11.  priority_queue<Stone>que; //定义一个Stone成员的优先//队列

阅读(748) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~