C++中 sort()和priority_queue()中的自定义比较

C++ sort / priority_queue自定义比较

sort / priority_queue的自定义比较是有区别的:

  • sort是自定义函数
  • priority_queue则是自定义结构体,结构体里面重载()实现自定义比较函数的功能

 

sort的使用方式

1. 创建自定义比较函数

static bool vec_cmp(const vector<int>& vec_a, const vector<int>& vec_b) {        // vec_cmp 是 vector_compare 的缩写
    return vec_a[1] < vec_b[1];
}


sort(vec1.begin(), vec1.end(), vec_cmp);

2. 自定义结构体并重载()运算符

struct vec_cmp_s{           // vec_cmp_s 是 vector_compare_struct 的缩写
    bool operator()(const vector<int>& vec_a, const vector<int>& vec_b) const {   
        return vec_a[1] < vec_b[1];
    }
};

sort(vec2.begin(), vec2.end(), vec_cmp_s());

其中注意,调用的时候写 vec_cmp_s(),相当于调用了结构体 vec_cmp_s 中的()函数,由于我们已经在结构体 vec_cmp_s 中将()重载成一个比较函数,因此我们可以得到和方法1中相同的结果。

 

priority_queue 的使用方式

优先队列的定义:

priority_queue<Type, Container, Functional>

Type 就是数据类型;

Container 就是容器类型(Container必须是用数组实现的容器,比如vector, deque等等,但不能用 list。STL里面默认用的是vector);

Functional 就是比较的方式,当需要用自定义的数据类型时才需要传入这三个参数,使用基本数据类型时,只需要传入数据类型,默认是大顶堆(即 std::less<>);

priority_queue <int,vector<int>,greater<int> > q;  //升序队列   

priority_queue <int,vector<int>,less<int> > q;     //降序队列

// greater 和 less 是 std 实现的两个仿函数(使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了)

 

优先队列使用方式:

1. 重载运算符 <

复制代码
struct tmp1 //运算符重载<
{
    int x;
    tmp1(int a) {x = a;}
    bool operator<(const tmp1& a) const
    {
        return x < a.x; //大顶堆
    }
};

int main()
{
    tmp1 a(1);
tmp1 b(2);
tmp1 c(3);
priority_queue<tmp1> pq;
...
}
复制代码

2. 重写仿函数()

struct heap_cmp_s{
    bool operator()(const vector<int>& vec_a, const vector<int>& vec_b) const {
        return vec_a[1] < vec_b[1];
    }
};

priority_queue<vector<int>, vector<vector<int>>, heap_cmp_s> hp;

注意,这里使用的是 heap_cmp_s 而不是 heap_cmp_s() ,因为 priority_queue的传入要求是自定义的数据结构,其中数据结构的里面要重载()

 代码如下:

复制代码
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

class Solution {
public:
    bool operator()(const pair<int, int>& a, const pair<int, int>& b) {
        if (a.first != b.first) {
            return a.first > b.first;           // great<> 即从小到大
        } else {
            return a.second < b.second;         // less<>  即从大到小
        }
    } 
};

void test()
{
    vector<pair<int, int>> ve{ {1,5}, {4,3}, {2,2}, {1,4}, {6,1} };
    priority_queue<pair<int, int>, vector<pair<int, int>>, Solution> pq;

    for (int i = 0; i < ve.size(); ++i) {
        pq.push(ve[i]);
    }

    while (!pq.empty()) {
        pair<int, int> temp = pq.top();
        cout << temp.first << " - " << temp.second << endl;
        pq.pop();
    }
}

int main()
{
    //Solution so;
    test();

    system("pause");
    return 0;
}
复制代码

 

 

以默认的优先队列为例:

template<typename _Tp, typename _Sequence = vector<_Tp>, typename _Compare  = less<typename _Sequence::value_type> >

其中仿函数 less 定义如下(本质上就是定义了一个泛型类,并在类中重载了()函数)

  template<typename _Tp>
    struct less : public binary_function<_Tp, _Tp, bool>
    {
      _GLIBCXX14_CONSTEXPR
      bool
      operator()(const _Tp& __x, const _Tp& __y) const
      { return __x < __y; }
    };

 

posted @   皮卡啰  阅读(599)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示