1--报错代码

class Mycompare{
public:
    bool operator()(const int &v1, const int &v2) {
        return v1 > v2; // 实现从大到小排序
    }
};

2--解决方法

class Mycompare{
public:
    bool operator()(const int &v1, const int &v2) const {
        return v1 > v2; // 实现从大到小排序
    }
};

3--原因分析

        C++17 标准规定 comparison object must be invocable as const,则参数列表必须加 const 修饰!

Logo

一站式 AI 云服务平台

更多推荐