[英]How do you access edge properties in the boost graph library
我试图编写一个包装升压图形库的程序,以便提供一个方便的用户界面。 我对提升(和堆栈溢出)非常陌生,但是花了很多时间阅读提升文档。
我的问题是,无论何时我要获取边缘属性的属性映射,无论是使用捆绑属性还是内部属性,调用边缘属性的get()
函数都说没有函数原型时会出错与给定参数匹配。 对顶点属性进行相同操作时,我没有任何困难。 此外,boost文档似乎还表明边缘属性没有get()
函数。 我认为问题可能出在我的构造函数中,该构造函数旨在从文本文件中读取图形信息:
BGraph::BGraph() // constructor
{
graph; // is this how you would initialize the graph?
//...input and initialization of vertices
//...input of edge information
auto e = add_edge(vertex1, vertex2, graph).first; // add the edge
// this is the internal property version
property_map<Graph, edge_weight_t>::type weightMap = get(edge_weight, graph);
// ^^^^^ error here saying there is no such member
weightMap[e] = inWeight;
//graph[e].weight = inWeight; this is the bundled version // give the edge its weight
}
}
这是包装类的头文件:
#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_iterator.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
#include <boost/graph/named_function_params.hpp>
#include <boost/graph/properties.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/graphviz.hpp>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
using namespace boost;
class BGraph
{
public:
struct Edge_Properties // property bundle for edges
{
string name;
int weight = 1;
};
struct Vertex_Properties // property bundle for vertices
{
string name;
int distance;
int pred;
};
// class member functions...
typedef adjacency_list<vecS, vecS, bidirectionalS, // graph type
Vertex_Properties, property<edge_weight_t, int> > Graph;
/*typedef adjacency_list<vecS, vecS, bidirectionalS, // graph type
Vertex_Properties, Edge_Properties> Graph;*/ // this is the bundled version
typedef property_map<Graph, vertex_index_t>::type IdMap;
typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
private:
Graph graph; // the boost graph
}
我的dijkstra_shortest_paths
函数有类似的问题:
dijkstra_shortest_paths(graph, findVertex(startVertex),
predecessor_map(get(&Vertex_Properties::pred, graph))
.distance_map(get(&Vertex_Properties::distance, graph))
.weight_map(/*get(&Edge_Properties::weight, graph)*/ get(edge_weight, graph)));
get函数的特定错误如下:
没有重载函数“ get”的实例与参数列表匹配。 参数类型为:
(boost:edge_weight_t, const BGraph::graph)
我觉得这里有一些过于简单的解决方案,但我只是找不到。 我正在使用MS Visual Studio 2017和增强版本boost_1_67_0。 我认为问题可能与Visual Studio有关,尤其是因为与我的代码几乎相同的代码似乎对其他人也有效。 谢谢你的帮助。
看来您的代码应适用于内部属性。
使用捆绑属性时,需要为property_map
指定PropertyTag
作为指向数据成员的指针,在这种情况下,指向Edge_properties
weight
成员的Edge_properties
。
property_map<Graph,int Edge_Properties::*>::type weightMap =
get(&Edge_Properties::weight, graph);
//^^^^^
auto e = add_edge(1, 2, graph).first;
weightMap[e] = 10;
graph[e].weight = 20;
这是从捆绑的属性获取属性映射的示例。 您可以轻松地使它们适应您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.