filtered-copy-example.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 <boost/graph/copy.hpp>
  11. #include <boost/graph/adjacency_list.hpp>
  12. #include <boost/graph/filtered_graph.hpp>
  13. #include <boost/graph/graph_utility.hpp>
  14. template <typename Graph>
  15. struct non_zero_degree {
  16. non_zero_degree() { } // has to have a default constructor!
  17. non_zero_degree(const Graph& g) : g(&g) { }
  18. bool operator()(typename boost::graph_traits<Graph>::vertex_descriptor v) const
  19. {
  20. return degree(v, *g) != 0;
  21. }
  22. const Graph* g;
  23. };
  24. int
  25. main()
  26. {
  27. using namespace boost;
  28. typedef adjacency_list < vecS, vecS, bidirectionalS,
  29. property < vertex_name_t, char > > graph_t;
  30. enum { a, b, c, d, e, f, g, N };
  31. graph_t G(N);
  32. property_map < graph_t, vertex_name_t >::type
  33. name_map = get(vertex_name, G);
  34. char name = 'a';
  35. graph_traits < graph_t >::vertex_iterator v, v_end;
  36. for (boost::tie(v, v_end) = vertices(G); v != v_end; ++v, ++name)
  37. name_map[*v] = name;
  38. typedef std::pair < int, int >E;
  39. E edges[] = { E(a, c), E(a, d), E(b, a), E(b, d), E(c, f),
  40. E(d, c), E(d, e), E(d, f), E(e, b), E(e, g), E(f, e), E(f, g)
  41. };
  42. for (int i = 0; i < 12; ++i)
  43. add_edge(edges[i].first, edges[i].second, G);
  44. print_graph(G, name_map);
  45. std::cout << std::endl;
  46. clear_vertex(b, G);
  47. clear_vertex(d, G);
  48. graph_t G_copy;
  49. copy_graph(make_filtered_graph(G, keep_all(), non_zero_degree<graph_t>(G)), G_copy);
  50. print_graph(G_copy, get(vertex_name, G_copy));
  51. return 0;
  52. }