0
点赞
收藏
分享

微信扫一扫

error: no match for ‘operator<‘ (operand types are ‘Interval‘ and ‘Interval‘)(sort函数出错(不能省略第三个参数))

struct Interval {
int start;
int end;
Interval(int s, int e) : start(start), end(e) {}
};

void insertInterval(vector<Interval>& Intervals, Interval newInterval)
{
sort(Intervals.begin(),Intervals.end());
}

出现编译错误,无法用sort对自定义结构体进行排序
解决方法:只能自定义排序函数

struct Interval {
int start;
int end;
Interval(int s, int e) : start(start), end(e) {}
};

static bool comp(const Interval & a, const Interval & b)
{
return a.start< b.start;
}
void insertInterval(vector<Interval>& Intervals, Interval newInterval)
{
sort(Intervals.begin(),Intervals.end(),comp);
}


举报

相关推荐

0 条评论