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

风流意气都作罢 提交于 2019-12-01 06:18:41

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 <boost\graph\adjacency_list.hpp>

int main()
{
    //Define graph
    typedef boost::adjacency_list
        <
            boost::vecS                                        //! edge list 
          , boost::vecS                                        //! vertex list
          , boost::undirectedS                                 //! undirected graph  
          , boost::property<boost::vertex_name_t, std::string> //! vertex properties : name                
          , boost::property<boost::edge_weight_t, int>         //! edge properties : weight 
        >   ScafGraph;

    //descriptors
    typedef boost::graph_traits<ScafGraph>::vertex_descriptor SV;
    typedef boost::graph_traits<ScafGraph>::edge_descriptor SE;

    //Graph Object
    ScafGraph SG;

    //property accessors
    boost::property_map<ScafGraph,
        boost::vertex_name_t>::type vertName = boost::get(boost::vertex_name, SG);
    boost::property_map<ScafGraph,
        boost::vertex_index_t>::type vertIndx = boost::get(boost::vertex_index, SG);
    boost::property_map<ScafGraph,
        boost::edge_weight_t>::type edgeWeight = boost::get(boost::edge_weight, SG);

    //Populate Graph
    std::vector<SV> 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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!