カスタム型比較関数

priority_queueカスタムタイプ、比較関数の処理メソッド

#include <bits/stdc++.h>

using namespace std; 

struct Node{
    
    
    int val;
    int cnt;
    Node(int a, int b): val(a), cnt(b){
    
    }
};

struct cmp{
    
    
    bool operator()(const Node& a, const Node& b){
    
    
        return a.cnt < b.cnt;//按照cnt的降序排列
    }
};

struct Node1{
    
    
    int val;
    int cnt;
    Node1(int a, int b): val(a), cnt(b){
    
    }
    bool operator<(const Node1& a)const{
    
    
        return cnt < a.cnt;//降序排列
    }
};

int main(){
    
    
    //方法一,定义仿函数用来对自定义类型的比较
    priority_queue<Node, vector<Node>, cmp> pq;

    //方法二,在自定义类型中重载小于比较运算符
    priority_queue<Node1, vector<Node1>> pq1;

    //测试仿函数
    cout << "测试仿函数" << endl;
    pq.push(Node(1, 4));
    pq.push(Node(2, 9));
    pq.push(Node(3, 1));
    pq.push(Node(4, 10));

    while(!pq.empty()){
    
    
        cout << pq.top().val << " " << pq.top().cnt << endl;
        pq.pop();
    }

    //测试自定义比较运算符
    cout << "测试自定义比较运算符" << endl;
    pq1.push(Node1(1, 4));
    pq1.push(Node1(2, 9));
    pq1.push(Node1(3, 1));
    pq1.push(Node1(4, 10));

    while(!pq1.empty()){
    
    
        cout << pq1.top().val << " " << pq1.top().cnt << endl;
        pq1.pop();
    }
    return 0;
}

演算結果:

测试仿函数
4 10
2 9
1 4
3 1
测试自定义比较运算符
4 10
2 9
1 4
3 1

マップでカスタムタイプの比較関数を処理する方法

#include <bits/stdc++.h>

using namespace std; 

struct Node{
    
    
    int val;
    int cnt;
    Node(int a, int b): val(a), cnt(b){
    
    }
};

struct cmp{
    
    
    bool operator()(const Node& a, const Node& b){
    
    
        return a.cnt < b.cnt;//按照cnt的升序排列
    }
};

struct Node1{
    
    
    int val;
    int cnt;
    Node1(int a, int b): val(a), cnt(b){
    
    }
    bool operator<(const Node1& a)const{
    
    
        return cnt < a.cnt;//升序排列
    }
};

int main(){
    
    
    //方法一,定义仿函数用来对自定义类型的比较
    map<Node, int, cmp> m;

    //方法二,在自定义类型中重载小于比较运算符
    map<Node1, int> m1;

    //测试仿函数
    cout << "测试仿函数" << endl;
    m[Node(1,4)] = 1;
    m[Node(2,10)] = 1;
    m[Node(3,7)] = 1;
    m[Node(-1,15)] = 1;

    for(auto temp : m){
    
    
        cout << temp.first.val << " " << temp.first.cnt << endl;
    }

    //测试自定义比较运算符
    cout << "测试自定义比较运算符" << endl;
    m1[Node1(1,4)] = 1;
    m1[Node1(2,10)] = 1;
    m1[Node1(3,7)] = 1;
    m1[Node1(-1,15)] = 1;

    for(auto temp : m1){
    
    
        cout << temp.first.val << " " << temp.first.cnt << endl;
    }
    return 0;
}

演算結果:

测试仿函数
1 4
3 7
2 10
-1 15
测试自定义比较运算符
1 4
3 7
2 10
-1 15

特別な注意:
ファンクタを定義するために同じ方法を使用し、クラスで比較演算子をオーバーロードすると、結果は昇順と降順になります。

カスタムタイプの比較のためのソート関数

#include <bits/stdc++.h>

using namespace std; 
/***********************************************************************/
struct Node{
    
    
    int val;
    int cnt;
    Node(int a, int b): val(a), cnt(b){
    
    }
};

struct cmp{
    
    
    bool operator()(const Node& a, const Node& b){
    
    
        return a.cnt < b.cnt;//按照cnt的升序排列
    }
};

bool cmp1(const Node& a, const Node& b){
    
    
    return a.cnt < b.cnt;
}
/**********************************************************/
struct Node1{
    
    
    int val;
    int cnt;
    Node1(int a, int b): val(a), cnt(b){
    
    }
    bool operator<(const Node1& a)const{
    
    
        return cnt < a.cnt;//升序排列
    }
};

int main(){
    
    
    vector<Node> nums;
    nums.push_back(Node(1, 4));
    nums.push_back(Node(3, 14));
    nums.push_back(Node(2, 9));
    nums.push_back(Node(5, 3));

    //sort(nums.begin(), nums.end(), cmp()); //采用仿函数的比较形式,注意这里的cmp要加上括号
    sort(nums.begin(), nums.end(), cmp1);//采用普通比较函数的形式
    for(auto t : nums){
    
    
        cout << t.val << " " << t.cnt << endl;
    }

    cout << "重载自定义类型的比较运算符" << endl;
    vector<Node1> nums1;
    nums1.push_back(Node1(1, 4));
    nums1.push_back(Node1(3, 14));
    nums1.push_back(Node1(2, 9));
    nums1.push_back(Node1(5, 3));
    sort(nums1.begin(), nums1.end());
    for(auto t : nums1){
    
    
        cout << t.val << " " << t.cnt << endl;
    }
    return 0;
}

演算結果:

5 3
1 4
2 9
3 14
重载自定义类型的比较运算符
5 3
1 4
2 9
3 14

おすすめ

転載: blog.csdn.net/qq_31672701/article/details/108454695