简体   繁体   English

BGL中边的自定义属性

[英]custom properties for edges in the BGL

I started using the BGL for some graph-related task. 我开始使用BGL进行一些与图形相关的任务。 I have a large number of edges and each edge has several properties, one of which is its weight. 我有很多边缘,每条边都有几个属性,其中一个是它的重量。 (All properties are floats and ints). (所有属性都是浮点数和整数)。 Since I never worked with the BGL (and/or similar CPP libraries) before, I'm a bit lost with all these types, classes and how to use it properly. 由于我之前从未使用过BGL(和/或类似的CPP库),所以我对所有这些类型,类以及如何正确使用它有点迷茫。

I add my edges like this: 我像这样添加边缘:

struct EdgeProperty
{
    int weight;
    float e1;
    float e2;
};

typedef adjacency_list<vecS, vecS, bidirectionalS, no_property, EdgeProperty> Graph;
...
EdgeProperty prop;
node1 = ...;
node2 = ...;
prop.e1 = ...;
prop.e2 = ...;
prop.weight = ...;

add_edge(node1, node2, prop, g);

Then, I need to access my property later, what I am trying to do like this: 然后,我需要稍后访问我的属性,我想这样做:

property_map<Graph, EdgeProperty>::type EdgeWeightMap = get(EdgeProperty, g);
w = get(EdgeWeightMap,some_edge);

However, this doesn't even compile. 但是,这甚至都没有编译。 It says in the error message: 它在错误消息中说:

error: no type named 'kind' in 'struct EdgeProperty'

amongst other errors, which I consider less important right now. 其他错误,我认为现在不那么重要。 I don't know if this is how you would use custom properties. 我不知道你是否会使用自定义属性。 Could you please explain to me the kind error message and how to use custom properties? 你能否向我解释一下kind错误信息以及如何使用自定义属性? I couldn't find any documentation (which I understand) on this topic. 我找不到关于这个主题的任何文档(我理解)。

Take a look at this code, I believe it explains a few things of its own: 看看这段代码,我相信它解释了一些自己的东西:

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/properties.hpp>
#include <iostream>

namespace bgl = boost;

struct EdgeInfo
{
    int weight;
    float e1;
    float e2;
};

struct EdgeInfoPropertyTag
{
    typedef bgl::edge_property_tag kind;
    static std::size_t const num; // ???
};

std::size_t const EdgeInfoPropertyTag::num = (std::size_t)&EdgeInfoPropertyTag::num;

typedef bgl::property<EdgeInfoPropertyTag, EdgeInfo> edge_info_prop_type;
typedef bgl::adjacency_list<bgl::vecS, bgl::vecS, bgl::bidirectionalS,
    bgl::no_property, edge_info_prop_type> Graph;
typedef bgl::graph_traits<Graph>::vertex_descriptor vertex_descr_type;
typedef bgl::graph_traits<Graph>::edge_descriptor edge_descr_type;

int
main ()
{
    Graph g;
    vertex_descr_type u, v;
    u = add_vertex (g);
    v = add_vertex (g);
    EdgeInfo props;
    props.weight = 3;
    std::pair<edge_descr_type, bool> result = add_edge (u, v, props, g);

    EdgeInfo p = get (EdgeInfoPropertyTag (), g, result.first);
    std::cout << "weight: " << p.weight << std::endl;
}

You need to read about the concepts that BGL is based upon. 您需要了解BGL所基于的概念。

This way you can hang any kind of value off of the edge (and similarly for vertex). 通过这种方式,您可以将任何类型的值悬挂在边缘上(对于顶点也是如此)。 You could also use the predefined types of properties, like edge_weight_t or edge_name_t I believe. 你也可以使用预定义的属性类型,比如edge_weight_tedge_name_t

See also BGL docs about custom edge properties . 另请参阅有关自定义边缘属性的 BGL文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM