jan*_*ver 5 c++ boost boost-graph
我开始使用BGL进行一些与图形相关的任务.我有很多边缘,每条边都有几个属性,其中一个是它的重量.(所有属性都是浮点数和整数).由于我之前从未使用过BGL(和/或类似的CPP库),所以我对所有这些类型,类以及如何正确使用它有点迷茫.
我像这样添加边缘:
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);
Run Code Online (Sandbox Code Playgroud)
然后,我需要稍后访问我的属性,我想这样做:
property_map<Graph, EdgeProperty>::type EdgeWeightMap = get(EdgeProperty, g);
w = get(EdgeWeightMap,some_edge);
Run Code Online (Sandbox Code Playgroud)
但是,这甚至都没有编译.它在错误消息中说:
error: no type named ‘kind’ in ‘struct EdgeProperty’
其他错误,我认为现在不那么重要.我不知道你是否会使用自定义属性.你能否向我解释一下kind错误信息以及如何使用自定义属性?我找不到关于这个主题的任何文档(我理解).
看看这段代码,我相信它自己解释了一些事情:
#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;
}
Run Code Online (Sandbox Code Playgroud)
您需要阅读 BGL 所基于的概念。
通过这种方式,您可以将任何类型的值挂在边缘之外(对于顶点也类似)。您还可以使用预定义类型的属性,例如edge_weight_t或edge_name_t我相信。
另请参阅有关自定义边属性的BGL 文档。