-------------------------------------
典型例题1 :C++优先级队列中,比较函数使用的两种情况:
-------------------------------------
1)比较符号重载法;
1 #include<iostream>
2 #include<vector>
3 #include<queue>
4 using namespace std;
5
6 struct node
7 {
8 int x;
9 int y;
10 int w;
11 friend bool operator> (const node& a,const node& b)
12 {
13 return (a.w > b.w);//按照y值递增;
14 }
15 friend bool operator< (const node& a,const node& b)
16 {
17 return (a.w < b.w);//按照w值递减;
18 }
19 };
20
21 int main()
22 {
23 int i;
24 priority_queue < node, vector<node>, greater<node> > que1;
25 priority_queue < node, vector<node>, less<node> > que2;
26 node da[5];
27 for(i = 0;i < 5;i++)
28 {
29 da[i].x = i;
30 da[i].y = 5-i;
31 da[i].w = i;
32 que1.push(da[i]);
33 que2.push(da[i]);
34 }
35
36 while(!que1.empty())
37 {
38 cout<<que1.top().x<<" "<<que1.top().y<<" "<<que1.top().w<<endl;
39 que1.pop();
40 }
41 cout<<endl;
42
43 while(!que2.empty())
44 {
45 cout<<que2.top().x<<" "<<que2.top().y<<" "<<que2.top().w<<endl;
46 que2.pop();
47 }
48 cout<<endl;
49 return 0;
50 }
-------------------------------------
haiping@ubuntu:~/program/yd0826$ ./a.out
0 5 0
1 4 1
2 3 2
3 2 3
4 1 4
0 5 0
1 4 1
2 3 2
3 2 3
4 1 4
2)自定义函数法;
1 #include<iostream>
2 #include<vector>
3 #include<functional>
4 #include<queue>
5 using namespace std;
6
7 struct node
8 {
9 int x;
10 int y;
11 int w;
12 };
13
14 struct node_greater_cmp
15 {
16 bool operator()(const node& a,const node& b)
17 {
18 return a.w > b.w;
19 }
20 };
21
22 struct node_less_cmp
23 {
24 bool operator()(const node& a,const node& b)
25 {
26 return a.w < b.w;
27 }
28 };
29
30
31 int main()
32 {
33 int i;
34 priority_queue < node, vector<node>, node_greater_cmp > que1;
35 priority_queue < node, vector<node>, node_less_cmp > que2;
36
37
38 node da[5];
39 for(i = 0;i < 5;i++)
40 {
41 da[i].x = i;
42 da[i].y = i;
43 da[i].w = 5-i;
44 que1.push(da[i]);
45 que2.push(da[i]);
46 }
47
48 while(!que1.empty())
49 {
50 cout<<que1.top().x<<" "<<que1.top().y<<" "<<que1.top().w<<endl;
51 que1.pop();
52 }
53 cout<<endl;
54
55 while(!que2.empty())
56 {
57 cout<<que2.top().x<<" "<<que2.top().y<<" "<<que2.top().w<<endl;
58 que2.pop();
59 }
60 cout<<endl;
61 return 0;
62 }
-------------------------------------
haiping@ubuntu:~/program/yd0826$ ./a.out
4 4 1
3 3 2
2 2 3
1 1 4
0 0 5
0 0 5
1 1 4
2 2 3
3 3 2
4 4 1
-------------------------------------
1 #include <iostream>
2 #include <fstream>
3 #include <vector>
4 #include <algorithm>
5 using namespace std;
6
7 struct node {
8 int a;
9 int b;
10 double c;
11 };
12
13 bool comp(node x,node y)
14 {
15 if(x.a!=y.a) return x.a<y.a;//首先根据a的小-大排列
16 if(x.b!=y.b) return x.b<y.b;//然后根据b的小-大排列
17 return x.c>y.c;//如果a,b都相同,按照c的大-小排列
18 }
19
20 int main(int argc, char * argv[])
21 {
22 ifstream cin("aaa.txt");
23 vector<node>v;
24 node nod;
25 int n;
26
27 while(cin>>n)
28 {
29 if(n == 0) break;
30 for(int i = 0;i<n;++i)
31 {
32 cin>>nod.a>>nod.b>>nod.c;
33 v.push_back(nod);
34 }
35 }
36 sort(v.begin(),v.end(),comp);
37
38 for(unsigned int i = 0;i<v.size();i++)
39 {
40 cout<<v[i].a<<" /t";
41 }
42 cout<<endl;
43 cout<<"---------------------"<<endl;
44
45 for(unsigned int i = 0;i<v.size();i++)
46 {
47 cout<<v[i].b<<" /t";
48 }
49 cout<<endl;
50 cout<<"---------------------"<<endl;
51
52
53 for(unsigned int i = 0;i<v.size();i++)
54 {
55 cout<<v[i].c<<" /t";
56 }
57 cout<<endl;
58 cout<<"---------------------"<<endl;
59
60 return 0;
61 }
-------------------------------------
haiping@ubuntu:~/program/yc0825$ ./a.out
1 1 1 2 6 6 7 8
-------------------------------------
3 3 6 2 2 2 3 1
-------------------------------------
3.1 1.3 9.7 3.1 5.6 1.6 1.2 6.9
-------------------------------------
C++优先级队列中,比较函数使用的两种情况:
最新推荐文章于 2024-08-18 19:23:29 发布