【问题标题】:Boost graph library using two visitors使用两个访问者提升图形库
【发布时间】:2012-10-05 19:06:21
【问题描述】:

经过一些测试,我发现 stamp_times 访问者是问题所在:

typedef adjacency_list <vecS, vecS, undirectedS> Graph;
typedef graph_traits <Graph>::edge_descriptor Edge;
typedef graph_traits <Graph>::vertex_descriptor Vertex;


Graph g(edges.begin(), edges.end(), n);

typedef graph_traits <Graph>::vertices_size_type Size;

std::vector<Size> dtime(num_vertices(g));
Size time = 0;

breadth_first_search(g, s, visitor(make_bfs_visitor(
                       stamp_times(dtime.begin(), time, on_discover_vertex()))));

(我在该代码中遇到的相同错误更少)。

我需要使用两个访问者,一是记录前辈,二是获取访问时间。

boost::breadth_first_search
    (g, s,
     boost::visitor(boost::make_bfs_visitor
            (std::make_pair(
            boost::record_predecessors(&p[0], boost::on_tree_edge()),
            stamp_times(dtime.begin(), time, on_discover_vertex())))));

但是这个代码事件不能编译。我收到以下错误。

/usr/include/boost/graph/visitors.hpp: In member function ‘void boost::time_stamper<TimeMap, TimeT, Tag>::operator()(Vertex, const Graph&) [with Vertex = long unsigned int, Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS>, TimeMap = __gnu_cxx::__normal_iterator<long unsigned int*, std::vector<long unsigned int> >, TimeT = long unsigned int, Tag = boost::on_discover_vertex]’:
/usr/include/boost/graph/visitors.hpp:109:8:   instantiated from ‘void boost::detail::invoke_dispatch(Visitor&, T, Graph&, mpl_::true_) [with Visitor = boost::time_stamper<__gnu_cxx::__normal_iterator<long unsigned int*, std::vector<long unsigned int> >, long unsigned int, boost::on_discover_vertex>, T = long unsigned int, Graph = const boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS>, mpl_::true_ = mpl_::bool_<true>]’
/usr/include/boost/graph/visitors.hpp:140:5:   instantiated from ‘void boost::invoke_visitors(Visitor&, T, Graph&, Tag) [with Visitor = boost::time_stamper<__gnu_cxx::__normal_iterator<long unsigned int*, std::vector<long unsigned int> >, long unsigned int, boost::on_discover_vertex>, T = long unsigned int, Graph = const boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS>, Tag = boost::on_discover_vertex]’

【问题讨论】:

  • 错误是:error: no matching function for call to 'put(__gnu_cxx::__normal_iterator&lt;long long unsigned int*, std::vector&lt;long long unsigned int&gt; &gt;&amp;, long long unsigned int&amp;, long long unsigned int&amp;)'。将dtime.begin() 更改为&amp;dtime[0] 即可解决此问题。

标签: c++ boost boost-graph


【解决方案1】:

stamp_times(和其他 EventVisitor)需要 WritablePropertyMap。根据thisproperty_traits 的特殊化允许使用 c++ 指针作为属性映射,并且根据this,“由于 std::vector 的迭代器(通过调用 begin() 获得)是一个指针,指针属性映射方法也适用于 std::vector::iterator"。显然,这最后一部分不适用于最新版本的 g++(在 4.6.3 和 4.7.1 上测试)。所以为了调用stamp_times,你需要使用&amp;dtime[0]而不是dtime.begin()

【讨论】:

  • 好的。我是对的。非常感谢不过。我错误地认为,如果某件事是用手册写的,那是正确的。
  • 我希望我对这个答案的支持不止一个,它解决了我的很多 BGL 问题和困惑。
猜你喜欢
  • 1970-01-01
  • 2021-09-07
  • 2018-03-04
  • 1970-01-01
  • 1970-01-01
  • 2011-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多