优先队列

£神魔★判官ぃ 2022-01-07 06:27 433阅读 0赞
  1. 1 priority_queue<int, vector<int>, cmp > q2;
  2. 2 struct cmp{
  3. 3 bool operator () (const int &a, const int &b){
  4. 4 return a>b;
  5. 5 }
  6. 6 };
  7. 7
  8. 8
  9. 9
  10. 10 #include <queue>
  11. 11 using namespace std;
  12. 12
  13. 13 struct cmp{
  14. 14 bool operator ()(int a,int b){ //通过传入不同类型来定义不同类型优先级
  15. 15 return a>b; //最小值优先
  16. 16 }
  17. 17 };
  18. 18 /**
  19. 19 struct cmp{
  20. 20 bool operator ()(int a,int b){
  21. 21 return a<b; //最大值优先
  22. 22 }
  23. 23 };
  24. 24 **/
  25. 25
  26. 26
  27. 27 priority_queue<int, vector<int>, cmp > q
  28. 28
  29. 29 #include <iostream>
  30. 30 #include <queue>
  31. 31 #include <vector>
  32. 32 #include <cstdio>
  33. 33 #include <cstring>
  34. 34
  35. 35 using namespace std;
  36. 36 struct cmp1
  37. 37 {
  38. 38 bool operator()(int x, int y)
  39. 39 {
  40. 40 return x > y;//小的优先级高
  41. 41 }
  42. 42 };
  43. 43
  44. 44 struct node
  45. 45 {
  46. 46 int x;
  47. 47 int y;
  48. 48 friend bool operator <(const node &a, const node &b)
  49. 49 {
  50. 50 return a.x > b.x;//小的优先级高
  51. 51 }
  52. 52 };
  53. 53 priority_queue<int, vector<int>,cmp1>q2;
  54. 54 priority_queue<node>q3;
  55. 55 int main()
  56. 56 {
  57. 57 int n;
  58. 58 scanf("%d", &n);
  59. 59 for(int i = 0; i < n; i ++)
  60. 60 {
  61. 61 node a;
  62. 62 cin>>a.x>>a.y;
  63. 63 q3.push(a);
  64. 64 }
  65. 65 cout<<endl;
  66. 66 while(!q3.empty())
  67. 67 {
  68. 68 cout<<q3.top().x<<" "<<q3.top().y<<endl;
  69. 69 q3.pop();
  70. 70 }
  71. 71
  72. 72 return 0;
  73. 73 }

转载于:https://www.cnblogs.com/coodyz/p/10596865.html

发表评论

表情:
评论列表 (有 0 条评论,433人围观)

还没有评论,来说两句吧...

相关阅读

    相关 队列优先队列

    队列 队列是一种特殊的[线性表][Link 1],特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作

    相关 优先队列

    优先队列:顾名思义,首先它是一个队列,但是它强调了“优先”二字,所以,已经不能算是一般意义上的队列了,它的“优先”意指取队首元素时有一定的选择性,即根据元素的属性选择某一项值最

    相关 优先队列

    [为什么80%的码农都做不了架构师?>>> ][80_] ![hot3.png][] 优先队列是不同于先进先出队列的另一种队列。每次从队列中取出的是具有最高优先权的元素。