0

我计划将我自己的比较功能与 boost bimap 一起使用。我要解决的问题是,当我将 boost bimap 与指针一起使用时,比较不应该比较两个指针,而应该比较指针指向的类。

我尝试了以下代码。但它甚至没有编译。我究竟做错了什么?还有一种更简单的方法来实现比较两个对象而不是两个指针的更少功能)

typedef std::set<int> ruleset;

template <class myclass>
bool comp_pointer(const myclass &lhs, const myclass &rhs)
{
    return ((*lhs) < (*rhs));
}

typedef boost::bimap<set_of<ruleset *, comp_pointer<ruleset *> >, int> megarulebimap;

错误信息:

party1.cpp:104:64:错误:“模板结构 boost::bimaps::set_of”的模板参数列表中参数 2 的类型/值不匹配 party1.cpp:104:64:错误:预期类型,得到“comp_pointer” 'party1.cpp:104:70: 错误:模板参数 1 无效 party1.cpp:104:85: 错误:';'之前的声明类型无效 令牌

4

1 回答 1

3
typedef std::set<int> ruleset;

struct ruleset_cmp {
    bool operator()(const ruleset *lhs, const ruleset *rhs) const
    {
        return ((*lhs) < (*rhs));
    }
};

typedef boost::bimap<set_of<ruleset *, ruleset_cmp>, int> megarulebimap;

好的。上面的代码片段有效。看来这里需要使用函子。

于 2013-06-15T07:25:17.310 回答