在我的项目中,我使用一个文件来存储所有边;另一个存储边缘概率。我想对图形使用 Boost
库,对概率使用 unordered_map
。我有以下代码。
typedef boost::adjacency_list <boost::vecS, boost::vecS, boost::bidirectionalS> DiGraph;
typedef boost::graph_traits<SubGraph>::vertex_descriptor vertex_t;
typedef boost::graph_traits<SubGraph>::edge_descriptor edge_t;
unordered_map<edge_t, double> P;
ifstream infile(prob_filename);
double p;
int u, v;
while (infile >> u >> v >> p) {
P[make_pair(u, v)] = p;
}
不过,我不需要做一对作为键,而是一个边描述符edge_t
。如何使用给定的两个值 u 和 v 创建边缘描述符。
最佳答案
DirectedGraph digraph(V);
{
boost::graph_traits<DirectedGraph>::vertex_descriptor u, v;
u = vertex(0, digraph); // read these instead 0,1 from from your file
v = vertex(1, digraph);
// populate graph
add_edge(digraph, u, v, Weight(1.2)); // read weight (prob) instead from from your file
boost::graph_traits<DirectedGraph>::edge_descriptor e;
bool found;
boost::tie(e, found) = edge(u, v, digraph);
//use property map
property_map<DirectedGraph, edge_weight_t>::type
weight = get(edge_weight, digraph);
cout << "weight[(u,v)] = " << get(weight, e) << endl;
}
关于c++ - 创建没有图形的边缘描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34070195/