family_tree.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //=======================================================================
  2. // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //=======================================================================
  8. #include <boost/config.hpp>
  9. #include <iostream>
  10. #include <vector>
  11. #include <string>
  12. #include <boost/graph/adjacency_list.hpp>
  13. #include <boost/tuple/tuple.hpp>
  14. enum family
  15. { Jeanie, Debbie, Rick, John, Amanda, Margaret, Benjamin, N };
  16. int
  17. main()
  18. {
  19. using namespace boost;
  20. const char *name[] = { "Jeanie", "Debbie", "Rick", "John", "Amanda",
  21. "Margaret", "Benjamin"
  22. };
  23. adjacency_list <> g(N);
  24. add_edge(Jeanie, Debbie, g);
  25. add_edge(Jeanie, Rick, g);
  26. add_edge(Jeanie, John, g);
  27. add_edge(Debbie, Amanda, g);
  28. add_edge(Rick, Margaret, g);
  29. add_edge(John, Benjamin, g);
  30. graph_traits < adjacency_list <> >::vertex_iterator i, end;
  31. graph_traits < adjacency_list <> >::adjacency_iterator ai, a_end;
  32. property_map < adjacency_list <>, vertex_index_t >::type
  33. index_map = get(vertex_index, g);
  34. for (boost::tie(i, end) = vertices(g); i != end; ++i) {
  35. std::cout << name[get(index_map, *i)];
  36. boost::tie(ai, a_end) = adjacent_vertices(*i, g);
  37. if (ai == a_end)
  38. std::cout << " has no children";
  39. else
  40. std::cout << " is the parent of ";
  41. for (; ai != a_end; ++ai) {
  42. std::cout << name[get(index_map, *ai)];
  43. if (boost::next(ai) != a_end)
  44. std::cout << ", ";
  45. }
  46. std::cout << std::endl;
  47. }
  48. return EXIT_SUCCESS;
  49. }