我是 BGL 的新手,我在制作自己的属性映射时遇到问题,键是 edge_property
你能告诉我下面的代码打印不出来我做错了什么吗: (0,1) == (0,1) ? 1个 但 (0,1) == (0,1) ? 0
这是代码
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
#include <map>
using namespace std;
class A {};
class B {};
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::bidirectionalS,
A, B > Graph;
typedef boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;
typedef boost::graph_traits<Graph>::edge_descriptor edge_descriptor;
typedef boost::graph_traits<Graph>::edge_iterator edge_iterator;
typedef boost::graph_traits<Graph>::in_edge_iterator in_edge_iterator;
typedef boost::graph_traits<Graph>::out_edge_iterator out_edge_iterator;
map<edge_descriptor, int> fun(Graph g){
map<edge_descriptor, int> m;
m[*(edges(g).first)] = 5;
return m;
}
int main(){
Graph g;
vertex_descriptor a = add_vertex(g);
vertex_descriptor b = add_vertex(g);
add_edge(a, b, g);
map<edge_descriptor, int> m = fun(g);
edge_iterator ei, ei_end;
for(tie(ei, ei_end) = edges(g) ; ei !=ei_end ; ++ei){
cout << m.begin()->first << " == " << *ei << " ? " << (m.begin()->first == *ei) << endl;
}
}
非常感谢!
编辑: 也许有比通过 edge_property 值映射边缘更好的方法?
最佳答案
您的边描述符有 3 个成员:源节点、目标节点和指向 B 属性的指针。在你的fun
你复制了你的图形,新图形中复制的边指向不同的 B。如果你声明你的 fun
作为map<edge_descriptor, int> fun(const Graph& g)
(您可能应该这样做,因为更大图表的拷贝可能很昂贵)您获得了预期的结果。
关于c++ - bgl 中的 edge_descriptor 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13488994/