优先队列

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<vector>
  5 using namespace std;
  6 
  7 //定义比较结构
  8 struct cmp1
  9 {
 10     bool operator ()(int &a,int &b)
 11     {
 12         return a>b;//最小值优先
 13     }
 14 };
 15 
 16 struct cmp2
 17 {
 18     bool operator ()(int &a,int &b)
 19     {
 20         return a<b;//最大值优先
 21     }
 22 };
 23 
 24 //自定义数据结构
 25 struct number1
 26 {
 27     int x;
 28     bool operator < (const number1 &a) const
 29     {
 30         return x>a.x;//最小值优先
 31     }
 32 };
 33 struct number2
 34 {
 35     int x;
 36     bool operator < (const number2 &a) const
 37     {
 38         return x<a.x;//最大值优先
 39     }
 40 };
 41 int a[]= {14,10,56,7,83,22,36,91,3,47,72,0};
 42 number1 num1[]= {14,10,56,7,83,22,36,91,3,47,72,0};
 43 number2 num2[]= {14,10,56,7,83,22,36,91,3,47,72,0};
 44 
 45 int main()
 46 {
 47     priority_queue<int>que;//采用默认优先级构造队列
 48 
 49     priority_queue<int,vector<int>,cmp1>que1;//最小值优先
 50     priority_queue<int,vector<int>,cmp2>que2;//最大值优先
 51 
 52     priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误,
 53     priority_queue<int,vector<int>,less<int> >que4;////最大值优先
 54 
 55     priority_queue<number1>que5; //最小优先级队列
 56     priority_queue<number2>que6;  //最大优先级队列
 57 
 58     int i;
 59     for(i=0; a[i]; i++)
 60     {
 61         que.push(a[i]);
 62         que1.push(a[i]);
 63         que2.push(a[i]);
 64         que3.push(a[i]);
 65         que4.push(a[i]);
 66     }
 67     for(i=0; num1[i].x; i++)
 68         que5.push(num1[i]);
 69     for(i=0; num2[i].x; i++)
 70         que6.push(num2[i]);
 71 
 72 
 73     printf("采用默认优先关系:\n(priority_queue<int>que;)\n");
 74     printf("Queue 0:\n");
 75     while(!que.empty())
 76     {
 77         printf("%3d",que.top());
 78         que.pop();
 79     }
 80     puts("");
 81     puts("");
 82 
 83     printf("采用结构体自定义优先级方式一:\n(priority_queue<int,vector<int>,cmp>que;)\n");
 84     printf("Queue 1:\n");
 85     while(!que1.empty())
 86     {
 87         printf("%3d",que1.top());
 88         que1.pop();
 89     }
 90     puts("");
 91     printf("Queue 2:\n");
 92     while(!que2.empty())
 93     {
 94         printf("%3d",que2.top());
 95         que2.pop();
 96     }
 97     puts("");
 98     puts("");
 99     printf("采用头文件functional内定义优先级:\n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)\n");
100     printf("Queue 3:\n");
101     while(!que3.empty())
102     {
103         printf("%3d",que3.top());
104         que3.pop();
105     }
106     puts("");
107     printf("Queue 4:\n");
108     while(!que4.empty())
109     {
110         printf("%3d",que4.top());
111         que4.pop();
112     }
113     puts("");
114     puts("");
115     printf("采用结构体自定义优先级方式二:\n(priority_queue<number>que)\n");
116     printf("Queue 5:\n");
117     while(!que5.empty())
118     {
119         printf("%3d",que5.top());
120         que5.pop();
121     }
122     puts("");
123     printf("Queue 6:\n");
124     while(!que6.empty())
125     {
126         printf("%3d",que6.top());
127         que6.pop();
128     }
129     puts("");
130     return 0;
131 }
132 /*
133 运行结果 :
134 采用默认优先关系:
135 (priority_queue<int>que;)
136 Queue 0:
137 91 83 72 56 47 36 22 14 10  7  3
138 
139 采用结构体自定义优先级方式一:
140 (priority_queue<int,vector<int>,cmp>que;)
141 Queue 1:
142 3 7 10 14 22 36 47 56 72 83 91
143 Queue 2:
144 91 83 72 56 47 36 22 14 10  7  3
145 
146 采用头文件"functional"内定义优先级:
147 (priority_queue<int,vector<int>,greater<int>/less<int> >que;)
148 Queue 3:
149 3 7 10 14 22 36 47 56 72 83 91
150 Queue 4:
151 91 83 72 56 47 36 22 14 10  7  3
152 
153 采用结构体自定义优先级方式二:
154 (priority_queue<number>que)
155 Queue 5:
156 3 7 10 14 22 36 47 56 72 83 91
157 Queue 6:
158 91 83 72 56 47 36 22 14 10  7  3
159 */
View Code
两个重要表示方法:
struct cmp { bool operator () (int &x,int &y) { return x > y;//小的优先级高 } };
priority_queue<int,vector<int>,cmp> q;


struct Number
{
    int x;
    bool operator < (const Number& a) const
    {
        return x > a.x;
    }
};
priority_queue <Number> q;
posted @ 2015-04-04 19:02  PastLIFE  阅读(123)  评论(0编辑  收藏  举报