On C++ Boost Graph Creation and the vertex_index Property.

后端 未结 1 1779
情歌与酒
情歌与酒 2021-01-13 09:35

I am boost noob. I am wondering why compilation fails in the following code. I am creating a set of vertices, and trying to assign my own vertex indices and vertex names. (I

1条回答
  •  遥遥无期
    2021-01-13 10:31

    The expression vertIndx[v] returns a Vertex by value. Thus you get the error because it's not an lvalue when you try to assign to it.

    Furthermore, it actually returns v. Here's the code run by vertIndx[v]:

    inline value_type operator[](key_type v) const { return v; }
    

    Here's a version that is hopefully clear about how it works:

    #include 
    
    int main()
    {
        //Define graph
        typedef boost::adjacency_list
            <
                boost::vecS                                        //! edge list 
              , boost::vecS                                        //! vertex list
              , boost::undirectedS                                 //! undirected graph  
              , boost::property //! vertex properties : name                
              , boost::property         //! edge properties : weight 
            >   ScafGraph;
    
        //descriptors
        typedef boost::graph_traits::vertex_descriptor SV;
        typedef boost::graph_traits::edge_descriptor SE;
    
        //Graph Object
        ScafGraph SG;
    
        //property accessors
        boost::property_map::type vertName = boost::get(boost::vertex_name, SG);
        boost::property_map::type vertIndx = boost::get(boost::vertex_index, SG);
        boost::property_map::type edgeWeight = boost::get(boost::edge_weight, SG);
    
        //Populate Graph
        std::vector svlist;
        for (int i = 0; i < 4; i++) {
            SV v = boost::add_vertex(ScafGraph::vertex_property_type(std::to_string(i)), SG);
            svlist.push_back(v);
            assert(vertName[v] == std::to_string(i));
            assert(vertIndx[v] == i);
        }
        return 0;
    }
    

    0 讨论(0)
提交回复
热议问题