我有一個頂點A..C一個向重和邊E1..E4Boost圖庫有向重edge_range錯誤
A ---E1--> B
A ---E2--> B
A ---E3--> B
B ---E4--> C
我想遍歷連接A中的邊緣和B.
在BGL,我表示這是:
#include <boost/graph/adjacency_list.hpp>
struct Vertex
{
std::string code;
};
struct Edge
{
double distance;
std::string code;
};
int main()
{
using namespace boost;
typedef adjacency_list<listS, vecS, directedS, Vertex, Edge> Graph;
Graph g;
auto a= add_vertex(Vertex{ "A" }, g);
auto b= add_vertex(Vertex{ "B" }, g);
auto c= add_vertex(Vertex{ "C" }, g);
add_edge(a, b, Edge{ 10, "E1" }, g);
add_edge(a, b, Edge{ 10, "E2" }, g);
add_edge(a, b, Edge{ 10, "E3" }, g);
add_edge(a, c, Edge{ 10, "E4" }, g);
// checking number of edges
std::cout<< num_edges(g)<< std::endl;
// printing edges branching from A
auto erange= out_edges(a, g);
for(auto i= erange.first; i!= erange.second; ++ i)
std::cout<< g[*i].code<< std::endl;
// now we want to iterate over edges that connect A and B
auto wtf= boost::edge_range(a, b, g);
}
這會導致編譯錯誤:
In file included from /usr/include/boost/graph/adjacency_list.hpp:246:
/usr/include/boost/graph/detail/adjacency_list.hpp:1617:25: error: no matching constructor for initialization of 'StoredEdge' (aka
'boost::detail::stored_edge_property<unsigned long, Edge>')
equal_range(el, StoredEdge(v, fake_edge_container.end(),
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我讀過的文檔:
std::pair<out_edge_iterator, out_edge_iterator> edge_range(vertex_descriptor u, vertex_descriptor v, const adjacency_list& g) Returns a pair of out-edge iterators that give the range for all the parallel edges from u to v. This function only works when the OutEdgeList for the adjacency_list is a container that sorts the out edges according to target vertex, and allows for parallel edges. The multisetS selector chooses such a container.
(http://www.boost.org/doc/libs/1_54_0/libs/graph/doc/adjacency_list.html)
修改圖表:
typedef adjacency_list<multisetS, vecS, directedS, Vertex, Edge> Graph;
但錯誤沒有改變。
那麼如何使用BGL在定向多圖中列出兩個頂點之間的邊(from→to)呢?
我發現了一個快速和骯髒的方式:
auto erange= out_edges(a, g);$
for(auto i= erange.first; i!= erange.second; ++ i)$
std::cout<< g[*i].code<< " -> "<< g[target(*i, g)].code<< std::endl;$
,這將讓我的目標頂點過濾邊緣。但你如何使用boost::edge_range
?
我試過了一個無向圖。在我的申請中,結果非常有趣。 – nurettin
這太糟糕了,我可以理解這種煩惱。您一定要聯繫圖書館的作者或Boost郵件列表。如果您也嘗試實施單獨的_dispatch()'重載並提交錯誤修復的建議,這可能會成功。 – TemplateRex
現在應該在r86469的Boost trunk中修復。將錯誤報告發送到boost-users郵件列表或者通過https://svn.boost.org/trac/boost提交,可以加快將來修復這類問題的速度。 –