• what's wrong about the `sort(intervals.begin(),intervals.end(),Comp);` ?
the error is :

``````Line 18: base operand of '->' has non-pointer type 'const Interval'
``````

Line 18 is in the function `bool Comp(const Interval &a,const Interval &b)`

``````/**
* Definition for an interval.
* struct Interval {
*     int start;
*     int end;
*     Interval() : start(0), end(0) {}
*     Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
int max(int a,int b){
if(a>b)
return a;
return b;
};
bool Comp(const Interval &a,const Interval &b){
return a->start<b->start;
};
vector<Interval> merge(vector<Interval> &intervals) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
sort(intervals.begin(),intervals.end(),Comp);
Interval tmp=intervals.begin();
vector<Interval> r;
vector<Interval>::iterator i;
for(i=intervals->begin()+1;i<intervals->end();i++){
if(tmp->end>intervals[i]->start){
tmp->end=max(tmp->end,*i->end);
}
else{
re.push_back(tmp);
tmp=intervals[i];
}
}
re.push_back(tmp);
return re;
}

};``````

• Use `.` rather than `->`, `Interval` is type of an object, not type of a pointer.

EDIT:

I suggest you read the documentation of the `sort()` and the documentation of the STL `vector`.

``````/**
* Definition for an interval.
* struct Interval {
*     int start;
*     int end;
*     Interval() : start(0), end(0) {}
*     Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool Comp(const Interval &a, const Interval &b){
return a.start<b.start;
};
class Solution {
public:
int max(int a,int b){
if(a>b)
return a;
return b;
};
vector<Interval> merge(vector<Interval> &intervals) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<Interval> re;
if(intervals.size()==0) return re;
sort(intervals.begin(),intervals.end(),Comp);
Interval tmp=*(intervals.begin());
vector<Interval>::iterator i;
for(i=intervals.begin(),i++;i!=intervals.end();i++){
if(tmp.end>=i->start){
tmp.end=max(tmp.end,i->end);
}
else{
re.push_back(tmp);
tmp=*i;
}
}
re.push_back(tmp);
return re;
}
};
``````

• that's right, but there is new error:Line 23: no matching function for call to 'sort(std::vector<Interval>::iterator, std::vector<Interval>::iterator, <unresolved overloaded function type>)'
am i use the sort() method in wrong way?

• I am not sure about this, there should be a `sort()` in `<algorithm>`

• change to:
static bool comp(const Interval &a, const Interval &b){
return (a.start < b.start);
}
Then it works. But I don't know why...Who can tell me why?

• Simply because a `comparator` must be static.

• I don't think so...When I write comparator without "static", it can run on my own computer, but can't pass the OJ

• `comparator` can be `global` or `static`. It cannot be a non `static` member function.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.