C++ 关于C++;Boost图形创建和顶点索引属性。

C++ 关于C++;Boost图形创建和顶点索引属性。,c++,boost,graph,boost-graph,C++,Boost,Graph,Boost Graph,我是诺布。我想知道为什么在下面的代码中编译失败。我正在创建一组顶点,并尝试指定自己的顶点索引和顶点名称。(我正在看这一页:) 我知道Boost中的vertS顶点列表不需要显式创建顶点id,我也在Stackoverflow()中看到了这个非常相关的问题,它讨论了如何使用关联属性映射来分配顶点索引。不过,下面的步骤——获取顶点索引映射,并分配键值对——似乎是一件相当简单的事情,我想了解它失败的原因。非常感谢您的帮助 编译错误如下: 错误:表达式不可赋值 vertIndx[v]=i //Define

我是诺布。我想知道为什么在下面的代码中编译失败。我正在创建一组顶点,并尝试指定自己的顶点索引和顶点名称。(我正在看这一页:)

我知道
Boost
中的
vertS
顶点列表不需要显式创建顶点id,我也在Stackoverflow()中看到了这个非常相关的问题,它讨论了如何使用
关联属性映射来分配顶点索引。不过,下面的步骤——获取顶点索引映射,并分配键值对——似乎是一件相当简单的事情,我想了解它失败的原因。非常感谢您的帮助

编译错误如下:

错误:表达式不可赋值
vertIndx[v]=i

//Define graph
typedef boost::property<boost::vertex_name_t, std::string> sv_namePty;
typedef boost::property<boost::vertex_index_t, int, sv_namePty > sv_indx_n_name_pty;
typedef boost::property<boost::edge_weight_t, int> se_weightPty;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, 
        sv_indx_n_name_pty, se_weightPty> 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(SG);
    svlist.push_back(v);
    vertName[v] = std::to_string(i);
    vertIndx[v] = i;
}
//定义图形
typedef boost::property sv_namePty;
typedef boost::property sv_indx_n_name_pty;
typedef boost::属性seu权重;
typedef boost::邻接列表ScafGraph;
//描述符
typedef boost::graph_traits::vertex_描述符SV;
typedef boost::graph_traits::edge_描述符SE;
//图形对象
ScafGraph-SG;
//属性访问器
boost::property\u map::type vertName=boost::get(boost::vertex\u name,SG);
boost::property\u map::type vertIndx=boost::get(boost::vertex\u index,SG);
boost::property\u map::type edgewight=boost::get(boost::edge\u weight,SG);
//填充图
std::向量svlist;

对于(int i=0;i表达式
vertIndx[v]
按值返回顶点。因此,您会得到错误,因为当您尝试分配给它时,它不是左值

此外,它实际上返回
v

inline value_type operator[](key_type v) const { return v; }
这里有一个版本,希望能清楚地了解它的工作原理:

#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;
}
#包括
int main()
{
//定义图
typedef boost::邻接列表
<
boost::vecS//!边缘列表
,boost::vecS/!顶点列表
,boost::无向图//!无向图
,boost::property/!顶点属性:name
,boost::property/!边缘属性:权重
>ScafGraph;
//描述符
typedef boost::graph_traits::vertex_描述符SV;
typedef boost::graph_traits::edge_描述符SE;
//图形对象
ScafGraph-SG;
//属性访问器
boost::property\u map::type vertName=boost::get(boost::vertex\u name,SG);
boost::property\u map::type vertIndx=boost::get(boost::vertex\u index,SG);
boost::property\u map::type edgewight=boost::get(boost::edge\u weight,SG);
//填充图
std::向量svlist;
对于(int i=0;i<4;i++){
SV v=boost::add_-vertex(ScafGraph::vertex_-property_-type(std::to_-string(i)),SG);
svlist。推回(v);
断言(vertName[v]==std::to_string(i));
断言(vertIndx[v]==i);
}
返回0;
}

属性映射很神秘,也很难使用。强烈建议您改用捆绑属性-它们更简单、更直观。我刚开始在您的评论后查看它;谢谢!它看起来确实更易于管理!非常感谢!1)这是否意味着我的代码中vertName的行为也不是我想要的(将名称分配给顶点描述符)?2)
ScafGraph::vertex_属性_类型(std::to_string(i))
i怀疑提供了顶点的名称;有没有可能用这样的构造来说明如何分配自己的索引而不是默认索引?谢谢我相信在添加顶点时设置name属性可能会稍微高效一些。至于自定义索引,我认为对于邻接列表,顶点描述符总是映射到索引值。这是因为它们是一体的。如果使用非随机访问的图形类型,它将使用void*作为描述符。在这种情况下,映射是非平凡的(将void*映射到索引)。我怀疑在那种情况下你也不能改变索引。您可以随时添加自己的自定义属性,并根据自己的喜好分配它们。有关如何分配所有3个属性
vertex\u name\u t
vertex\u index\u t
edge\u weight\u t
的说明会对这个答案有很大帮助。