我是一名 BGL 新手,有一个(可能)简单的问题:我有一个有向图并使用边缘的捆绑属性,其中之一是 int 类型的索引。知道唯一索引后,我想获取该边对应的edge_descriptor,以便对其执行操作。以下示例总结了我的问题:
#include <boost/graph/adjacency_list.hpp>
struct EdgeProperties {
EdgeProperties(): distance(10), time_limit(5) {};
int index;
int distance;
int time_limit;
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, boost::no_property, EdgeProperties> Graph;
int main() {
Graph graph;
EdgeProperties edge_prop1, edge_prop2, edge_prop3, edge_prop4;
// Define edge properties
edge_prop1.index = 0;
edge_prop2.index = 1;
edge_prop3.index = 2;
edge_prop4.index = 3;
// Add edges to graph
boost::add_edge(0, 1, edge_prop1, graph);
boost::add_edge(0, 2, edge_prop2, graph);
boost::add_edge(1, 3, edge_prop3, graph);
boost::add_edge(2, 3, edge_prop4, graph);
// Get vertex_descriptor from an (int) index:
int vertex_index = 2;
boost::graph_traits<Graph>::vertex_descriptor v = boost::vertex(vertex_index, graph);
// I would like to get an edge_descriptor from an (int) index property:
// The following DOES NOT work:
boost::graph_traits<Graph>::edge_descriptor e = boost::edge(edge_prop1.index, graph);
}
我也阅读了有关属性映射的内容,但找不到解决我的问题的方法。我更喜欢 bundled properties超过内部属性。 有没有办法通过捆绑属性将唯一的 int 类型索引分配给边并通过这些 int 类型值访问边?
最佳答案
遗憾的是,我认为 boost::graph
在这里没有立即帮助。
首先,没有机制可以根据边缘属性的字段来查找边缘(或顶点) - BGL 保留任何此类映射,并且您拥有的“索引”字段完全用于您的目的.
其次,有一个 boost::edges
函数,它返回图的所有边的迭代器范围。我认为您可以将 vecS 作为边缘容器类型传递给 adjacency_list 模板,然后查看此范围内的内容,但是按照 http://www.boost.org/doc/libs/1_61_0/libs/graph/doc/EdgeListGraph.html迭代器只需要是多遍输入迭代器,并且实现正是这样做的——即使使用 vecS 作为边缘类型,你也不能进行随机访问。
因此,似乎完成您想要的任务的唯一方法是保留您自己的从索引到边缘描述符的 unodered_map
。
关于c++ - Boost图库: Get edge_descriptor or access edge by index of type int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37336463/