【问题标题】:custom comparetor in stlstl中的自定义比较器
【发布时间】:2010-12-15 14:51:53
【问题描述】:

这已经让我发疯了 3 个小时。有人知道这不起作用的原因吗?

struct sortByPropRev
{
    bool operator()(const cust_type &a, const cust_type &b) const
    {
        return a.prop > b.prop;
    }
};
...
priority_queue<cust_type, vector<cust_type>, sortByPropRev> x;

我收到编译错误:
Error C2664: 'bool (cust_type &amp;,cust_type &amp;)' : cannot convert parameter 1 from 'const cust_type' to 'cust_type &amp;'

还有 2 个类似的,但在 algorithm.h 的不同行上

【问题讨论】:

  • struct cust_type {int a, int b, ...; cust_type(int a, int b, ...) : a(a), b(b) {} }

标签: stl priority-queue comparator


【解决方案1】:

你给了它 b.pprop,而不是 a.prop。我认为考虑到编译器未能正确解析结构定义的错误 - 检查其上方代码中的语法错误。

【讨论】:

    【解决方案2】:

    没关系。我发现了问题。它位于调用相同算法函数的代码的不同部分。很抱歉打扰大家,感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-07
      • 2021-07-12
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多