Modifying vertex properties in a Boost::Graph

前端 未结 5 1853
执念已碎
执念已碎 2020-12-04 09:40

I am trying to figure out how to use boost::graph to store some information. However, there is information I want tied to each vertex. Staring at the documentation for the l

5条回答
  •  [愿得一人]
    2020-12-04 09:57

    I consider Boost.Graph to have a very good documentation, but not truly for beginners on the matter. So here goes an example that i hope is simple enough !

    //includes
    
    // Create a name for your information
    struct VertexInformation
    {
      typedef boost::vertex_property_type type;
    };
    
    // Graph type, customize it to your needs
    // This is when you decide what information will be attached to vertices and/or edges
    // of MyGraph objects
    typedef boost::adjacency_list > MyGraph;
    
    int main()
    {
      MyGraph graph;
    
      // Create accessor for information
      typedef boost::property_map::type  InformationAccessor;
      InformationAccessor information( get( VertexInformation(), graph ) );
    
      // Create a vertex (for example purpose)
      typedef boost::graph_traits::vertex_descriptor MyVertex;
      MyVertex vertex = add_vertex( graph );
    
      // Now you can access your information
      put( information, vertex, 1. );
    
      // returns 1 !
      get( information, vertex );
      return 0;
    }
    

提交回复
热议问题