zoukankan      html  css  js  c++  java
  • zz STL 优先队列

       1: #include<stdio.h>
       2: #include<functional>
       3: #include<queue>
       4: #include<vector>
       5: using namespace std;
       6: //定义结构,使用运算符重载,自定义优先级1
       7: struct cmp1{
       8:     bool operator ()(int &a,int &b){
       9:         return a>b;//最小值优先
      10:     }
      11: };
      12: struct cmp2{
      13:     bool operator ()(int &a,int &b){
      14:         return a<b;//最大值优先
      15:     }
      16: };
      17: //定义结构,使用运算符重载,自定义优先级2
      18: struct number1{
      19:     int x;
      20:     bool operator < (const number1 &a) const {
      21:         return x>a.x;//最小值优先
      22:     }
      23: };
      24: struct number2{
      25:     int x;
      26:     bool operator < (const number2 &a) const {
      27:         return x<a.x;//最大值优先
      28:     }
      29: };
      30: int a[]={14,10,56,7,83,22,36,91,3,47,72,0};
      31: number1 num1[]={14,10,56,7,83,22,36,91,3,47,72,0};
      32: number2 num2[]={14,10,56,7,83,22,36,91,3,47,72,0};
      33:  
      34: int main()
      35: {    priority_queue<int>que;//采用默认优先级构造队列
      36:  
      37:     priority_queue<int,vector<int>,cmp1>que1;//最小值优先
      38:     priority_queue<int,vector<int>,cmp2>que2;//最大值优先
      39:  
      40:     priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
      41:                                                       //这是右移运算符,所以这里用空格号隔开
      42:     priority_queue<int,vector<int>,less<int> >que4;////最大值优先
      43:  
      44:     priority_queue<number1>que5;
      45:     priority_queue<number2>que6;
      46:  
      47:     int i;
      48:     for(i=0;a[i];i++){
      49:         que.push(a[i]);
      50:         que1.push(a[i]);
      51:         que2.push(a[i]);
      52:         que3.push(a[i]);
      53:         que4.push(a[i]);
      54:     }
      55:     for(i=0;num1[i].x;i++)
      56:         que5.push(num1[i]);
      57:     for(i=0;num2[i].x;i++)
      58:         que6.push(num2[i]);
      59:  
      60:  
      61:     printf("采用默认优先关系:/n(priority_queue<int>que;)/n");
      62:     printf("Queue 0:/n");
      63:     while(!que.empty()){
      64:         printf("%3d",que.top());
      65:         que.pop();
      66:     }
      67:     puts("");
      68:     puts("");
      69:  
      70:     printf("采用结构体自定义优先级方式一:/n(priority_queue<int,vector<int>,cmp>que;)/n");
      71:     printf("Queue 1:/n");
      72:     while(!que1.empty()){
      73:         printf("%3d",que1.top());
      74:         que1.pop();
      75:     }
      76:     puts("");
      77:     printf("Queue 2:/n");
      78:     while(!que2.empty()){
      79:         printf("%3d",que2.top());
      80:         que2.pop();
      81:     }
      82:     puts("");
      83:     puts("");
      84:     printf("采用头文件/"functional/"内定义优先级:/n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)/n");
      85:     printf("Queue 3:/n");
      86:     while(!que3.empty()){
      87:         printf("%3d",que3.top());
      88:         que3.pop();
      89:     }
      90:     puts("");
      91:     printf("Queue 4:/n");
      92:     while(!que4.empty()){
      93:         printf("%3d",que4.top());
      94:         que4.pop();
      95:     }
      96:     puts("");
      97:     puts("");
      98:     printf("采用结构体自定义优先级方式二:/n(priority_queue<number>que)/n");
      99:     printf("Queue 5:/n");
     100:     while(!que5.empty()){
     101:         printf("%3d",que5.top());
     102:         que5.pop();
     103:     }
     104:     puts("");
     105:     printf("Queue 6:/n");
     106:     while(!que6.empty()){
     107:         printf("%3d",que6.top());
     108:         que6.pop();
     109:     }
     110:     puts("");
     111:     return 0;
     112: }
     113: /*
     114: 运行结果 :
     115: 采用默认优先关系:
     116: (priority_queue<int>que;)
     117: Queue 0:
     118:  91 83 72 56 47 36 22 14 10  7  3
     119: 
     120: 采用结构体自定义优先级方式一:
     121: (priority_queue<int,vector<int>,cmp>que;)
     122: Queue 1:
     123:   3  7 10 14 22 36 47 56 72 83 91
     124: Queue 2:
     125:  91 83 72 56 47 36 22 14 10  7  3
     126: 
     127: 采用头文件"functional"内定义优先级:
     128: (priority_queue<int,vector<int>,greater<int>/less<int> >que;)
     129: Queue 3:
     130:   3  7 10 14 22 36 47 56 72 83 91
     131: Queue 4:
     132:  91 83 72 56 47 36 22 14 10  7  3
     133: 
     134: 采用结构体自定义优先级方式二:
     135: (priority_queue<number>que)
     136: Queue 5:
     137:   3  7 10 14 22 36 47 56 72 83 91
     138: Queue 6:
     139:  91 83 72 56 47 36 22 14 10  7  3
     140: */
  • 相关阅读:
    AJAX请求头Content-type
    原 layer父子页面交互
    layer.closeAll()无法关闭弹窗的解决办法之一
    成员函数的重载,覆盖与隐藏
    const限定符用法汇总
    构造函数和析构函数的调用时机
    MFC 消息映射、分派和传递
    C++对象模型
    函数指针与虚函数表
    数据类型与内存映像
  • 原文地址:https://www.cnblogs.com/bovine/p/2395901.html
Copyright © 2011-2022 走看看