About the sort(vector.begin(),vector.end(),comp)


  • 0
    J

    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;
        }
        
    };

  • 1
    P

    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;
        }
    };
    

  • 0
    J

    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?


  • 0
    P

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


  • 0
    Z

    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?


  • 0
    P

    Simply because a comparator must be static.


  • 0
    Z

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


  • 0
    P

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


Log in to reply
 

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