对vector进行sort时,cmp函数的写法
原文链接:https://blog.csdn.net/wusecaiyun/article/details/48263495
当cmp函数写在类外时,如下:
bool mycmp(const Interval& a, const Interval& b) // 注意comp函数可以为static bool或者bool,返回值为bool类型。a.start<b.start为升序
{
return a.start < b.start;
}
class Solution {
public:
void merge(vector<Interval>& intervals) {
sort(intervals.begin(), intervals.end(), comp);
}
};
写在类内时,如下:
class Solution {
private:
static bool comp(const Interval& a, const Interval& b) // 注意comp函数必须为static bool,返回值为bool类型。a.start<b.start为升序
{
return a.start < b.start;
}
public:
void merge(vector<Interval>& intervals) {
sort(intervals.begin(), intervals.end(), comp);
}