[英]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.