fork(2) download
  1. #include <boost/graph/adjacency_list.hpp>
  2. #include <boost/graph/breadth_first_search.hpp>
  3. #include <boost/graph/visitors.hpp>
  4.  
  5. using Graph = boost::adjacency_list<>;
  6. using Vertex = Graph::vertex_descriptor;
  7. using Edge = Graph::edge_descriptor;
  8.  
  9. namespace boost
  10. {
  11. template <typename event_type>
  12. class test_visitor: public default_bfs_visitor
  13. {
  14. public:
  15. using event_filter = event_type;
  16. void discover_vertex(Vertex, const Graph&) const
  17. {
  18. std::cout << "vertex discovered" << std::endl;
  19. }
  20. };
  21. }
  22.  
  23. int main()
  24. {
  25.  
  26. // Initialize a test graph
  27. Graph graph;
  28. Vertex vertexFoo = boost::add_vertex(graph);
  29. Vertex vertexBar = boost::add_vertex(graph);
  30. Vertex vertexBaz = boost::add_vertex(graph);
  31. boost::add_edge(vertexFoo, vertexBar, graph);
  32. boost::add_edge(vertexBar, vertexBaz, graph);
  33.  
  34. // Initialize parents map
  35. std::vector<Vertex> parents_map(boost::num_vertices(graph));
  36. parents_map[vertexFoo] = vertexFoo;
  37.  
  38. // Perform BFS with two listeners
  39. boost::breadth_first_search(
  40. graph,
  41. vertexFoo,
  42. boost::visitor(
  43. boost::make_bfs_visitor(
  44. std::make_pair(
  45. boost::record_predecessors(&parents_map[0], boost::on_tree_edge()),
  46. boost::test_visitor<boost::on_discover_vertex()>()
  47. )
  48. )
  49. )
  50. );
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Standard output is empty