提升图形通过vertex_descriptor的访问属性 [英] Boost Graph accessing properties through vertex_descriptor

查看:691
本文介绍了提升图形通过vertex_descriptor的访问属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的自定义顶点和边属性。

I have my custom vertex and edge properties

namespace boost { 
    enum vertex_diagonal_t{vertex_diagonal = 999};
    BOOST_INSTALL_PROPERTY(vertex, diagonal);
}
namespace boost { 
    enum edge_dominance_t{edge_dominance = 998};
    BOOST_INSTALL_PROPERTY(edge, dominance);
}

我创造我与邻接表的boost ::财产

typedef boost::adjacency_list<
      boost::listS, 
      boost::vecS,
      boost::bidirectionalS,
      boost::property<boost::vertex_diagonal_t, const khut::diagonal*>,
      boost::property<boost::edge_dominance_t,  float>
    > diagonal_dominance_graph;
typedef boost::property_map<diagonal_dominance_graph, boost::vertex_diagonal_t>::type diagonal_map_type;
typedef boost::property_map<diagonal_dominance_graph, boost::edge_dominance_t>::type  dominance_map_type;

现在我通过我自己的容器要循环,并添加顶点

Now I want to loop through my own containers and add vertex

diagonal_dominance_graph graph;
  for(storage_type::const_iterator i = repo_begining.begin(); i != repo_begining.end(); ++i){
    diagonal_dominance_graph::vertex_descriptor dia_vertex = boost::add_vertex(graph);

    //>> ?? HOW CAN I write Properties to dia_vertex HERE ?

    //boost::property<boost::vertex_diagonal_t, const khut::diagonal*> p;
    //boost::put(p, dia_vertex);

  }

什么我没有得到我是如何通过设置顶点属性顶点描述。可能是我缺少一个简单的函数。

What I am not getting is How can I set properties of a vertex through vertex_descriptor. may be I am missing a simple function.

请我不需要任何让BGL更加复杂,或者一些清洁和在我的例子重组的类型。我只需要知道如何通过读/写性能的 vertex_descriptor的 edge_descriptor的

Please I don't need anything that makes BGL even more complex, or something that cleans and restructures the types in my example. I just need to know how to read/write properties through a vertex_descriptor or edge_descriptor

推荐答案

您正在使用属性列表:它们是<一个href=\"http://www.boost.org/doc/libs/1_57_0/libs/graph/doc/using_adjacency_list.html#sec:custom-vertex-properties\"相对=nofollow>这里记录。

You're using property lists: they're documented here.

因此​​,在你的榜样,你会使用

So in your example, you'd use

diagonal_map_type  vp = get(boost::vertex_diagonal, graph);

using storage_type = std::vector<int>;
storage_type repo_begining(10);

for(storage_type::const_iterator i = repo_begining.begin(); i != repo_begining.end(); ++i) {
    diagonal_dominance_graph::vertex_descriptor dia_vertex = boost::add_vertex(graph);


    khut::diagonal* v = nullptr;
    boost::put(vp, dia_vertex, v);
}

// likewise for edges
dominance_map_type ep = get(boost::edge_dominance, graph);

看它的 住在Coliru

See it Live On Coliru

而在同文档页说:

注意:本BGL支持用于指定内部属性两个可互换的方法:绑定属性和属性列表。前者是更容易使用,并且需要较少的努力,而后者是与旧的,破碎的编译器兼容,并且与之前的1.32.0升压版本向后兼容。如果你绝对要求兼容性,请继续阅读,了解属性列表。 否则,我们强烈建议你阅读捆绑性机制。

NOTE: The Boost Graph Library supports two interchangeable methods for specifying interior properties: bundled properties and property lists. The former is easier to use and requires less effort, whereas the latter is compatible with older, broken compilers and is backward-compatible with Boost versions prior to 1.32.0. If you absolutely require these compatibility features, read on to learn about property lists. Otherwise, we strongly suggest that you read about the bundled properties mechanism.

1.32升压日期在10年前!所以,我建议绑定属性:

Boost 1.32 dates over 10 years ago! So, I'd suggest bundled properties:

<大骨节病> 住在Coliru

#include <boost/graph/adjacency_list.hpp>

namespace khut {
    struct diagonal { };

    struct MyVertexProperties {
        diagonal const* diag_ptr;
    };

    struct MyEdgeProperties {
        float dominance;
    };
}

typedef boost::adjacency_list<
      boost::listS, 
      boost::vecS,
      boost::bidirectionalS,
      khut::MyVertexProperties,
      khut::MyEdgeProperties
    > diagonal_dominance_graph;

#include <iostream>


int main() {
    using namespace boost;

    diagonal_dominance_graph g;

    khut::diagonal d1, d2;
    {
        auto v1 = add_vertex(khut::MyVertexProperties { &d1 }, g);
        auto v2 = add_vertex(khut::MyVertexProperties { &d2 }, g);

        /*auto e1 = */add_edge(v1, v2, khut::MyEdgeProperties { 42.31415926 }, g);
    }

    for(diagonal_dominance_graph::vertex_descriptor vd : make_iterator_range(vertices(g)))
        std::cout << "Is diagonal d1? " << std::boolalpha << (&d1 == g[vd].diag_ptr) << "\n";
    for(diagonal_dominance_graph::edge_descriptor ed : make_iterator_range(edges(g)))
        std::cout << "Edge dominance: " << g[ed].dominance << "\n";
}

打印

Is diagonal d1? true
Is diagonal d1? false
Edge dominance: 42.3142

这篇关于提升图形通过vertex_descriptor的访问属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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