相关疑难解决方法(0)

为什么std :: sort segfault与非传递比较器?

struct Object
{
    int x;
    int y;
    bool isXValid()
    {
       return x > 0;
    }
};

bool mySort(const Object& lhs, const Object& rhs)
{
    // Note the short-circuit here
    bool isValidForCheck = lhs.isXValid() && rhs.isXValid();

    // rhs may be valid because short-circuit, or both may be invalid
    if (isValidForCheck)
    {
       return lhs.x < rhs.x;
    }

    return lhs.y < rhs.y;
}

int main()
{
    std::vector<Object> myVec;
    // random populating of myVec
    std::sort(myVec.begin(), myVec.end(), mySort);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的比较函数是反射性的,但不尊重传递性.

stl排序 - 严格的弱排序 …

c++ sorting gcc gnu std

5
推荐指数
0
解决办法
237
查看次数

标签 统计

c++ ×1

gcc ×1

gnu ×1

sorting ×1

std ×1