C++, custom cmp for std::sort(), runtime error

  • 0

    Hi, I want to customize cmp for std::sort() in some cases. Like to sort Interval. Before I can use code like this:

    // before the Solution class, I define cmp
    bool cmp (Interval& lhs, Interval& rhs) {
    // I want to sort based on the start of Interval, and then the length of Interval
    return lhs.start<rhs.start || (lhs.start==rhs.start && (lhs.end-lhs.start)<=(rhs.end-rhs.start));
    // in Solution class, I just need to use

    This code works fine before, but has runtime error since several days ago.

    My question: any idea how to define a custom cmp without runtime error in current environment?

  • 0

    @wgl Hi, Could you please include the complete code here? Without the complete code it will be difficult to troubleshoot this issue. Thanks!

  • 0

    @1337c0d3r Hi, thank you very much for help. I have this error in LC435, as well as other problems. Please check the code:

    struct mycmp {
        inline bool operator() (const Interval& lhs,const Interval& rhs) {
            if (lhs.start<rhs.start) return true;
            if (lhs.start>rhs.start) return false;
            return (lhs.end-lhs.start)<=(rhs.end-rhs.start);
    class Solution {
        int eraseOverlapIntervals(vector<Interval>& intervals) {
        if (intervals.size()<2) return 0;
        int last=intervals[0].end,out=0;
        for (int i=1;i<intervals.size();i++) {
            if (intervals[i].start>=last) {
            else {
        return out;

    I use a similar code on Ubuntu 16.04 with g++/icpc without any problem. However on LeetCode I got this error:

    I used similar approach in LeetCode ago and it worked fine. I started to have this error several days ago.

    I wonder whether this is a bug of my code or something else. Thank you very much.

  • 0

    @wgl Hi, we recently upgraded our g++ compiler to the latest v6.3, that's why the Runtime Error was introduced.

    I found this error was output:

    Error: comparison doesn't meet irreflexive requirements, assert(!(a < a))
    Objects involved in the operation:
        instance "functor" @ 0x0x7ffc6098bd2f {
          type = mycmp;
        iterator::value_type "ordered type" {
          type = Interval;
    Aborted (core dumped)

    Hope this helps. I'll try to fix the issue with runtime error message not being displayed properly.

  • 0

    @1337c0d3r Thank you for your help!

Log in to reply

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