提升bfs predecessor_map问题

时间:2013-11-27 23:52:19

标签: c++ boost boost-graph

我正在尝试学习如何使用Boost Graph Library,但我遇到了一些问题。据我了解,以下代码中对boost::breadth_first_search的两次调用应该是等效的,但predecessor_map版本不起作用(p保持不变)。

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <vector>

int main() {

    typedef boost::adjacency_list<
        boost::vecS, boost::vecS, boost::undirectedS> Graph;

    typedef Graph::vertex_descriptor vertex_t;
    typedef std::pair<vertex_t, vertex_t> edge;

    edge edges[] = {
        edge(0, 1), edge(1, 2), edge(2, 3), edge(0, 4), edge(4, 5), edge(5, 6),
        edge(6, 7), edge(7, 8)
    };

    Graph g(edges, edges + sizeof(edges) / sizeof(edge), 9);
    std::cout << boost::num_vertices(g) << " vertices and " << boost::num_edges(g) << " edges" << std::endl;

    std::vector<vertex_t> p(boost::num_vertices(g)), d(boost::num_vertices(g));
    vertex_t s = *(boost::vertices(g).first);
    p[s] = s;

    boost::breadth_first_search(g, s, boost::predecessor_map(&p[0]));
    // this call works, but the above one doesn't
    //boost::breadth_first_search(g, s, boost::visitor(boost::make_bfs_visitor(boost::record_predecessors(&p[0], boost::on_tree_edge()))));

    for (auto itr = p.begin(); itr != p.end(); ++itr)
        std::cout << *itr << std::endl;
}

有人可以解释这两个电话之间的区别吗?为什么命名参数版本不起作用?另外,我在文档中找不到对此的答案,但是两个不同的访问者(例如predecessor_map和自定义访问者)可以处理相同的事件,例如on_tree_edge()

0 个答案:

没有答案