首页 文章

带边缘列表的自定义关联容器的BGL边(u,v,g)

提问于
浏览
3

我刚刚开始学习bgl并且在使用带有自定义排序的std :: set作为adjacency_list中边缘列表的容器时遇到了问题 . 我定义了operator <来根据它们的属性对边进行排序,就像在ordered_out_edges.cpp示例中一样 . 这里boost :: edge_unique_ordering是一个自定义属性标记 .

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)函数时,我得到了迭代器异常 . 如果我用一种解决方法替换这些调用以避免请求(源,目标)边缘,那么一切正常 .

我查看了增强代码,我很确定我知道原因是什么,我只是不确定这是否意味着我做错了什么,这是增强代码的问题,或者只是一个无证的不兼容性 . 该函数在u的外边列表容器上调用set :: find(StoredEdge(v)) . 现在默认的stored_edge :: operator <只比较目标顶点,但在我的情况下我的自定义运算符<正在被调用,并且正在查找的StoredEdge(v)显然默认初始化没有属性,这可能是导致问题 . 在我看来,边缘(u,v,g)应该严格地根据目标顶点搜索任何匹配,而不管容器内边缘的排序是什么 .

任何人都可以了解我可能做错了什么或不理解?

1 回答

  • 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());
      }
    };
    

    然后使用 target_compare<order_by_unique_order<Edge> > 作为 set 中的比较类型 .

相关问题