BGL边缘捆绑属性

时间:2013-06-08 16:26:41

标签: boost-graph

我正在使用捆绑属性,例如

class cVertex { ... };
class eEdge { ... };
typedef boost::adjacency_list <
    boost::vecS, boost::vecS, boost::undirectedS,
    cVertex, cEdge  >
            graph_t;
graph_t myGraph;

这适用于顶点。我可以编写代码来轻松访问顶点捆绑属性

const cVertex& v = myGraph[ *vertices(myGraph).first + idx ];

然而,同样的事情似乎对边缘不起作用

const cEdge& e = myGraph[ *edges(myGraph).first + idx ];

我收到这些编译错误

1>.\cGraph.cpp(109) : error C2678: binary '+' : 
no operator found which takes a left-hand operand of type 
'boost::detail::edge_desc_impl<Directed,Vertex>' 
(or there is no acceptable conversion)

我也试过这个:

对于顶点,这很有效

boost::graph_traits<graph_t>::vertex_iterator vi = vertices(myGraph).first;
vi += idx;

但这会给编译器错误

boost::graph_traits<graph_t>::edge_iterator  ei = edges(myGraph).first;
ei += idx;

这是错误

>C:\boost\boost_1_51\boost/iterator/iterator_adaptor.hpp(330) :
error C3767: '+=': candidate function(s) not accessible
1>        could be the friend function at 'C:\boost\boost_1_51\boost/graph/topology.hpp(63)' :
'+='  [may be found via argument-dependent lookup]

2 个答案:

答案 0 :(得分:1)

adjacency_list不包含图形的单个边矢量,但将边存储为...邻接列表。这意味着每个顶点将自己的边存储为相邻顶点的列表。

有许多其他数据结构来表示图形,例如edge_list将使您的边缘可直接访问,adjacency_matrix或压缩的稀疏行图(用于快速和紧凑的只读访问)。

您还应该能够通过创建自定义(属性)地图来直接访问边缘来解决您的问题。

答案 1 :(得分:-1)

我找到了这个解决方法

boost::graph_traits<graph_t>::edge_iterator  ei = edges(myGraph).first;

for( int k = 0; k < idx; k++ ) {
    ei++;
}

这是必要的,这似乎令人难以置信!!!

正如Jeremiah Willcock所建议的那样,通过编写

可以使代码看起来更简单
boost::graph_traits<graph_t>::edge_iterator  ei = edges(myGraph).first;
std::advance( ei, idx );

(实际上执行的代码是相同的,添加了关于迭代器是否是随机访问和函数调用本身的测试)