代码之家  ›  专栏  ›  技术社区  ›  Dom

boost图形库邻接列表:维护按EdgeProperties排序的EdgeList

  •  0
  • Dom  · 技术社区  · 10 年前

    我正在尝试使用一个std::multiset容器,在EdgeProperties上对boost::adacency_list的EdgeList模板参数进行严格的弱排序

    namespace boost {
    
    struct propOrderedMultisetS { };
    template <class ValueType>
    struct container_gen<propOrderedMultisetS,ValueType> {
        struct less {
            bool operator() (const ValueType& lhs, const ValueType& rhs) const {
                return (lhs.get_property() < rhs.get_property());
            };
        };
        typedef std::multiset<ValueType, less> type;
    };
    
    struct MyVertexProp { int v; };
    struct MyEdgeProp {
        bool operator<(const MyEdgeProp& rhs) const {
            return this->weight < rhs.weight;
        }
        double weight;
    }
    
    typedef adjacency_list<listS, listS, undirectedS, MyVertexProp, MyEdgeProp,
     no_property, propOrderedMultisetS> PropOrderedGraph;
    }
    
    using namespace boost;
    int main() {
        PropOrderedGraph g;
    
        // ... adding some vertices and edges
    
        for (auto e_range=edges(g); e_range.first != e_range.second; ++e_range.first) {
            // works! prints the edges ordered by weight
            std::cout << g[*e_range.first].weight << std::endl;
        }
    
        for (auto v_range=vertices(g); v_range.first != v_range.second; ++v_range.first) {
            // works! prints all vertices (random order)
            std::cout << g[*v_range.first].v << std::endl;
        }
    
        auto first_vertex = *vertices(g).first;
        for (auto adj_v_range=adjacent_vertices(first_vertex, g); adj_v_range.first != adj_v_range.second; ++adj_v_range.first) {
            // problem: dereferencing causes compiler error, see below
            std::cout << g[*adj_v_range.first].v << std::endl;
        }
    
        return 0;
    }
    

    在第三个for循环中取消对迭代器的引用会导致编译器错误:

    /usr/include/boost/graphic/detail/aximity_list.hpp:293:69:错误:MyEdgeProp&来自const MyEdgeProp类型的表达式 内联属性&get_property(){return m_iter->get_propery();}

    你知道我该如何纠正这个错误,或者我该如何完成任务吗?

    1 回复  |  直到 10 年前
        1
  •  0
  •   Dom    10 年前

    我更改了图形库,现在使用LEMON,它为我的任务提供了一个IterableValueMap类。