c++ - 使用两个访问者 boost 图形库

标签 c++ boost boost-graph

经过一些测试,我观察到 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]’

最佳答案

stamp_times (和其他 EventVisitor)期望 WritablePropertyMap 。根据this property_traits 的特殊化允许使用 C++ 指针作为属性映射,并根据 this “由于 std::vector 的迭代器(通过调用 begin() 获得)是一个指针,因此指针属性映射方法也适用于 std::vector::iterator”。显然,最后一部分对于最新版本的 g++ 来说是不正确的(在 4.6.3 和 4.7.1 上测试)。因此,为了调用 stamp_times,您需要使用 &dtime[0] 而不是 dtime.begin()

关于c++ - 使用两个访问者 boost 图形库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12901877/

相关文章:

c++ - Int 值急剧变化和段错误

c++ - 我可以在 Boost 中使用 std::function (C++11 lambda) 吗?

c++ - 获取错误:在抛出 'std::bad::alloc' what(): std::bad_alloc 的实例后调用终止

c++ - 证书颁发机构的 openssl 函数问题

c++ - Boost::Mpi 是否支持并行IO

qt - QScopedPointer 与 boost::scoped_ptr [选择哪一个]

c++ - 类成员的typedef

iterator - 在 Boost Graph Library 中,为什么添加边会使边迭代器失效(以及其他问题)?

c++ - 如何遍历有向图中的所有边并获取源+目标

c++ - 如何置换 boost::adjacency_list 中的节点?