代码之家  ›  专栏  ›  技术社区  ›  K. Shores

使用捆绑类型的Boost属性映射

  •  0
  • K. Shores  · 技术社区  · 6 年前

    我在获取我正在绘制的图形的属性映射时遇到问题。我正在使用捆绑属性。我将它简化为下面的示例。当我尝试为获取类型时收到一个错误 IndexMap . VC++编译器和GCC的错误都是 error forming a reference to void or illegal use of type void . 错误在Boost的内部 adjacency_list.hpp ,但是由我实例化 boost::property_map . 我一直无法理解正确的类型应该是什么。我读了Boost的文档 bundled properties 但发现它们有点没用。有什么想法吗?

    编辑:我使用的是Boost1.67.0。

    编辑2:显然改为 vecS 而不是 listS 对于顶点表示,可以修复此问题。不过,我更喜欢用 列表 因为我需要在遍历图时修改它。

    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/graph_traits.hpp>
    #include <boost/graph/properties.hpp>
    #include <string>
    #include <memory>
    #include <utility>
    #include <vector>
    
    struct MyVertex
    {
        std::string m_name;
    };
    
    int main()
    {
        using graph_t = boost::adjacency_list<boost::vecS, boost::listS, boost::undirectedS, MyVertex>;
        using Vertex = boost::graph_traits<graph_t>::vertex_descriptor;
        using IndexMap = boost::property_map<graph_t, boost::vertex_index_t>::type;
        std::unique_ptr<graph_t> graph;
        std::vector<std::pair<int, int>> edges{ {0,1}, {0,2}, {1,2}, {3,4}, {1,3}, {1,4}};
        graph = std::make_unique<graph_t>(edges.begin(), edges.end(), 5);
        IndexMap index = boost::get(boost::vertex_index, *graph);
        return 0;
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   sehe    6 年前

    就像我解释的那样 earlier today 您的图形没有顶点索引。如果你想要它有一个,你必须自己添加它。

    Live On Coliru

    #include <boost/graph/adjacency_list.hpp>
    
    struct MyVertex {
        int id;
        std::string name;
    };
    
    using graph_t = boost::adjacency_list<boost::vecS, boost::listS, boost::undirectedS, MyVertex>;
    using Vertex = graph_t::vertex_descriptor;
    
    int main() {
        graph_t g;
        auto v0 = add_vertex(MyVertex{0, "zero"},  g);
        auto v1 = add_vertex(MyVertex{1, "one"},   g);
        auto v2 = add_vertex(MyVertex{2, "two"},   g);
        auto v3 = add_vertex(MyVertex{3, "three"}, g);
        auto v4 = add_vertex(MyVertex{4, "four"},  g);
    
        for (auto [from, to] : { std::pair { v0, v1 }, { v0, v2 }, { v1, v2 }, { v3, v4 }, { v1, v3 }, { v1, v4 } }) {
            add_edge(from, to, g);
        }
    }
    

    现在可以使用ID作为顶点索引:

    auto index = get(&MyVertex::id, g);
    

    C++ 11中的PS.

    for (auto p : std::vector<std::pair<Vertex, Vertex> > { { v0, v1 }, { v0, v2 }, { v1, v2 }, { v3, v4 }, { v1, v3 }, { v1, v4 } }) {
        add_edge(p.first, p.second, g);
    }
    

    在C++ 03中写: Live On Coliru