【问题标题】:BGL edge(u, v, g) with custom associative container for edge listsBGL edge(u, v, g) 与边缘列表的自定义关联容器
【发布时间】:2012-02-07 00:13:23
【问题描述】:

我刚刚开始学习 bgl,并且在使用具有自定义排序的 std::set 作为 adjacency_list 中边缘列表的容器时遇到了问题。我定义 operator

template < typename Edge >
struct order_by_unique_order: public std::binary_function< Edge, Edge, bool >
{
    inline bool operator() (const Edge& e1, const Edge& e2) const
    {
        return boost::get(boost::edge_unique_ordering, e1) < boost::get(boost::edge_unique_ordering, e2);
    }
};

struct default_edge_containerS {};

namespace boost
{
    template < class ValueType >
    struct container_gen< default_edge_containerS, ValueType >
    {
        typedef std::set< ValueType, order_by_unique_order< ValueType > > type;
    };
}

一般来说它工作正常,但是当我使用 edge(u, v, g) 函数时,我得到了迭代器异常。如果我用解决方法替换这些调用以避免通过(源,目标)请求边缘,那么一切正常。

我查看了 boost 代码,我很确定我知道原因是什么,我只是不确定这是否意味着我做错了什么,这是 boost 代码的问题,或者只是一个未记录的不兼容性.该函数在 u 的外边列表容器上调用 set::find(StoredEdge(v))。现在默认的 stored_edge::operator

谁能解释一下我可能做错了什么或不理解?

【问题讨论】:

  • 如何设置容器类型?您是否使用container_traits 创建了自己的容器生成器标签?
  • 已编辑以显示容器规范。我猜你的意思是container_gen?我在 bgl 文档中的任何地方都没有遇到 container_traits。

标签: boost boost-graph


【解决方案1】:

看起来您需要编写一个包装比较运算符,该运算符接受一个类型(将使用StoredEdge 类型填充)并使用您的自定义比较函数比较两个输入上get_target) 的结果,使用类似的东西:

template <typename Cmp>
struct target_compare {
  Cmp cmp;
  target_compare(const Cmp& cmp): cmp(cmp) {}
  template <typename SE>
  bool operator()(const SE& a, const SE& b) const {
    return cmp(a.get_target(), b.get_target());
  }
};

然后在您的set 中使用target_compare&lt;order_by_unique_order&lt;Edge&gt; &gt; 作为比较类型。

【讨论】:

    猜你喜欢
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多