gpt4 book ai didi

c++ - boost 图形库定向 multimap edge_range 错误

转载 作者:太空狗 更新时间:2023-10-29 23:05:29 25 4
gpt4 key购买 nike

我有一个有向 multimap ,顶点为 A..C,边为 E1..E4

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 列出有向多重图中两个顶点(从 -> 到)之间的边?

我找到了一个快速而肮脏的方法:

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

最佳答案

此错误之前已在 Boost mailinglist 上报告过.

it fails to compile when the Directed Selector template argument to adjacency_list is set to directedS, but does work if the argument is either undirectedS, or bidirectionalS. Attached below is a short program illustrating the problem. The problem is that edge_range() instantiates a StoredEdge via a constructor taking 3 arguments, but when the Directed Selector is directedS StoredEdge is typedef'ed to stored_edge_property, which has no such constructor. One solution might be to create overloaded edge_range_dispatch() functions, and dispatch on
Config::on_edge_storage.

在您的程序中将 directedS 更改为 undirectedS 是可行的。 Live Example .但这可能不是您的应用程序所需要的,因此您之前提到的简单过滤器可能会更好。您可以在 Boost 邮件列表上重新发布它以获得更多关注。

关于c++ - boost 图形库定向 multimap edge_range 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19223692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com