参考自:3 Ways to Define Comparison Functions in C++
STL里有不少东西需要比较函数,例如 sort(),set<T>,priority_queue<T>。
下面我们提供三种比较函数。
1、定义 operator<():
使用该方法的前提是有默认的比较函数会调用我们的 operator<()。
比如我们有如下类,那么我们可以这样定义 operator<。
struct Edge{ int from, to, weight;};bool operator<(Edge a, Edge b){ //使用大于号实现小于号,表示排序顺序与默认顺序相反。若使用小于号实现小于号,则相同。可用此法方便理解。 return a.weight > b.weight;}
这样,我们就有了下面的例子。
#include <iostream>#include <algorithm>#include <set>#include <vector>#include <queue>using namespace std;struct Edge{ int from, to, weight; Edge(int from, int to, int weight):from(from), to(to), weight(weight){}};bool operator<(Edge a, Edge b){ return a.weight > b.weight;}int main(){ priority_queue<Edge> pq; pq.push(Edge(0, 1, 7)); pq.push(Edge(1, 2, 4)); pq.push(Edge(2, 3, 10)); pq.push(Edge(3, 4, 5)); //默认的优先队列是最大堆,使用我们定义的比较方法后反转为最小堆。 while(!pq.empty()) { cout << pq.top().weight << endl; pq.pop(); } vector<Edge> vec; vec.push_back(Edge(0, 1, 7)); vec.push_back(Edge(1, 2, 4)); vec.push_back(Edge(2, 3, 10)); vec.push_back(Edge(3, 4, 5)); sort(vec.begin(), vec.end()); //sort()函数默认为增序,这里反转为降序。 for(int i = 0; i < vec.size(); ++i) { cout << vec[i].weight << endl; } set<Edge> se; set<Edge>::iterator iter; se.insert(Edge(0, 1, 7)); se.insert(Edge(1, 2, 4)); se.insert(Edge(2, 3, 10)); se.insert(Edge(3, 4, 5)); //set默认为增序,这里反转为降序。 for(iter = se.begin(); iter != se.end(); ++iter) { cout << iter -> weight << endl; } return 0;}
对应的输出为:
457101075410754
2、定义一个普通的比较函数:
还是用前面的设定:
struct Edge{ int from, to, weight;};bool cmp(Edge a, Edge b){ return a.weight > b.weight;}//然后使用sort()函数sort(data.begin(), data.end(), cmp);
举例如下:
#include <iostream>#include <algorithm>#include <vector>using namespace std;struct Edge{ int from, to, weight; Edge(int from, int to, int weight):from(from), to(to), weight(weight){}};bool cmp(Edge a, Edge b){ return a.weight > b.weight;}int main(){ vector<Edge> vec; vec.push_back(Edge(0, 1, 7)); vec.push_back(Edge(1, 2, 4)); vec.push_back(Edge(2, 3, 10)); vec.push_back(Edge(3, 4, 5)); sort(vec.begin(), vec.end(), cmp); //sort()函数默认为增序,这里反转为降序。 for(int i = 0; i < vec.size(); ++i) { cout << vec[i].weight << endl; } return 0;}
3、定义 operator()():
operator()重载函数需要被定义(声明)在一个新的结构体内。如下:
#include <iostream>#include <set>#include <vector>using namespace std;struct cmp{ bool operator()(const int &a, const int &b) { return a > b; }};int main(){ set<int, cmp> se; set<int, cmp>::iterator iter; se.insert(7); se.insert(4); se.insert(10); se.insert(5); //sort()函数默认为增序,这里反转为降序。 for(iter = se.begin(); iter != se.end(); ++iter) { cout << *iter << endl; } return 0;}
三种方法,我都快记不太清楚了,这里总结如下:
1、operator<() 仅适用于自定义结构体(operator()()重载后形参必须要有结构体)。operator<() 函数的添加可以从容修改自带排序功能的容器(set, priority_queue等)的比较规则,在定义该容器时只需set<T>或priority_queue<T>即可,不需要添加其他参数。在使用sort()函数时也不用指定比较函数。
2、定义比较函数,目前本人只知道其用于STL里sort()的第三个参数。
3、operator()() 则适用于内建类型与自定义结构体(operator()()形参可以是内建数据类型)以及sort(),但需要类似这样定义容器set<T, cmp>或priority_queue<T, vector<T>, cmp>,其中cmp为仅包含operator()()函数的结构体。
后来发现这样并不是很便于记忆,可以按照容器类型记忆见: STL中sort、priority_queue、map、set的自定义比较函数
sort():operator<()、比较函数、operator()()
map, set, priority_queue:operator<()、operator()()
除此之外,我们在使用sort()或定义容器时,还可以使用greater<T>和less<T>,当T为内建类型时,注意在sort使用中需要在<T>后额外加()。