在C ++ Boost图创建和vertex_index属性。 [英] On C++ Boost Graph Creation and the vertex_index Property.

查看:274
本文介绍了在C ++ Boost图创建和vertex_index属性。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是增强noob。我想知道为什么编译失败在下面的代码。我创建一组顶点,并尝试分配我自己的顶点索引和顶点名称。 (我关注此页面: http://fireflyblue.blogspot.com/ 2008/01 / boost-graph-library.html 。)

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 am following this page: http://fireflyblue.blogspot.com/2008/01/boost-graph-library.html. )

我理解 vertS 列表 Boost 不需要显式的顶点id创建,我也看到了这个非常相关的问题在Stackoverflow(如何为我的图提供一个vertex_index属性),讨论如何使用 associative_property_map 来分配顶点索引。以下虽然 - 获取vertex_index映射,并分配键值对 - 似乎是一个相当简单的事情,我想了解为什么它失败。任何帮助,非常感谢!

I understand that vertS vertex lists in Boost does not need explicit vertex id creations, and I have also seen this very related question in Stackoverflow (how provide a vertex_index property for my graph) which discusses how to use an associative_property_map to assign vertex indices. The following though - getting the vertex_index map, and assigning the key value pairs - seems a fairly straightforward thing to do, and I would like to understand why it fails. Any help is greatly appreciated!

编译错误如下:

错误:表达式无法分配
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;
}


推荐答案

c> vertIndx [v] 按值返回顶点。

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.

此外,它实际上会返回 v 。下面是 vertIndx [v] 运行的代码:

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;
}

这篇关于在C ++ Boost图创建和vertex_index属性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆