C++關於sort和priority_queue的運算符重載

C++中的sort函數默認是將元素升序排列的,而priority_queue默認是將元素降序排列的(默認實現的是大頂堆)。ios

自定義運算符用的比較多,如下2種對sort和priority_queue運算符的重載都有效,效果都是同樣的:函數

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <queue>
 5 using namespace std;
 6 
 7 //sort實現的都是先按a值降序排列,a相同時,按b值降序排列
 8 //priority_queue和sort的默認排序相反,實現的都是先按a值升序排列,a相同時,按b值升序排列
 9 
10 //重載方式一:
11 struct Node {
12     int a, b;
13     Node(int x, int y) {
14         a = x;
15         b = y;
16     }
17     bool operator < (const Node &c) const {
18         if(a == c.a) return b > c.b;
19         return a > c.a;
20     }
21 };
22 
23 //重載方式二:注意要有public
24 // class Node {
25 //     public:
26 //     int a, b;
27 //     Node(int x, int y) {
28 //         a = x;
29 //         b = y;
30 //     }
31 //     bool operator < (const Node &c) const {
32 //         if(a == c.a) return b > c.b;
33 //         return a > c.a;
34 //     }
35 // };
36 int main() {
37     vector<Node> v;
38     v.push_back(Node(1,2));
39     v.push_back(Node(1,4));
40     v.push_back(Node(4,2));
41     v.push_back(Node(3,3));
42     sort(v.begin(), v.end());
43     for(int i = 0; i < v.size(); ++i) {
44         cout<<v[i].a<<" "<<v[i].b<<endl;
45     }
46     cout<<endl;
47     
48     priority_queue<Node> pq;
49     pq.push(Node(1,2));
50     pq.push(Node(1,4));
51     pq.push(Node(4,2));
52     pq.push(Node(3,3));
53     while(!pq.empty()) {
54         cout<<pq.top().a<<" "<<pq.top().b<<endl;
55         pq.pop();
56     }
57     return 0;
58 }

 

輸出spa

4 2
3 3
1 4
1 2

1 2
1 4
3 3
4 2

 

對sort單獨自定義運算符:code

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <queue>
 5 using namespace std;
 6 
 7 //sort實現的都是先按a值降序排列,a相同時,按b值降序排列
 8 //priority_queue和sort的默認排序相反,實現的都是先按a值升序排列,a相同時,按b值升序排列
 9 
10 struct Node {
11     int a, b;
12     Node(int x, int y) {
13         a = x;
14         b = y;
15     }
16 };
17 /*
18 //重載方式一:
19 struct compare {
20     bool operator()(const Node a, const Node b) {
21         if(a.a == b.a) return a.b > b.b;
22         return a.a > b.a;
23     }
24 } cmp;
25 */
26 
27 /*
28 //重載方式二:
29 bool cmp(const Node a, const Node b) {
30     if(a.a == b.a) return a.b > b.b;
31     return a.a > b.a;
32 }
33 */
34 
35 int main() {
36     
37     vector<Node> v;
38     v.push_back(Node(1,2));
39     v.push_back(Node(1,4));
40     v.push_back(Node(4,2));
41     v.push_back(Node(3,3));
42     //重載方式一和二:
43     sort(v.begin(), v.end(), cmp);
44     //重載方式三:
45     sort(v.begin(), v.end(), [](const Node a, const Node b) {
46         if(a.a == b.a) return a.b > b.b;
47         return a.a > b.a;
48     });
49     for(int i = 0; i < v.size(); ++i) {
50         cout<<v[i].a<<" "<<v[i].b<<endl;
51     }
52     cout<<endl;
53     return 0;
54 }

 

輸出都是blog

4 2
3 3
1 4
1 2

 

對priority_queue單獨自定義運算符:排序

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

//sort實現的都是先按a值降序排列,a相同時,按b值降序排列
//priority_queue和sort的默認排序相反,實現的都是先按a值升序排列,a相同時,按b值升序排列

struct Node {
    int a, b;
    Node(int x, int y) {
        a = x;
        b = y;
    }
};

struct cmp {
    bool operator()(const Node a, const Node b) {
        if(a.a == b.a) return a.b > b.b;
        return a.a > b.a;
    }
};

int main() {
    priority_queue<Node, vector<Node>, cmp> pq;
    
    pq.push(Node(1,2));
    pq.push(Node(1,4));
    pq.push(Node(4,2));
    pq.push(Node(3,3));
    while(!pq.empty()) {
        cout<<pq.top().a<<" "<<pq.top().b<<endl;
        pq.pop();
    }
    
    return 0;
}

 

效果:it

1 2
1 4
3 3
4 2
相關文章
相關標籤/搜索