interior_property_map.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. #include <boost/config.hpp>
  10. #include <iostream>
  11. #include <algorithm>
  12. #include <boost/graph/adjacency_list.hpp>
  13. #include <boost/property_map/property_map.hpp>
  14. #include <string>
  15. using namespace std;
  16. using namespace boost;
  17. /*
  18. Interior Property Map Basics
  19. An interior property map is a way of associating properties
  20. with the vertices or edges of a graph. The "interior" part means
  21. that the properties are stored inside the graph object. This can be
  22. convenient when the need for the properties is somewhat permanent,
  23. and when the properties will be with a graph for the duration of its
  24. lifetime. A "distance from source vertex" property is often of this
  25. kind.
  26. Sample Output
  27. Jeremy owes Rich some money
  28. Jeremy owes Andrew some money
  29. Jeremy owes Jeff some money
  30. Jeremy owes Kinis some money
  31. Andrew owes Jeremy some money
  32. Andrew owes Kinis some money
  33. Jeff owes Jeremy some money
  34. Jeff owes Rich some money
  35. Jeff owes Kinis some money
  36. Kinis owes Jeremy some money
  37. Kinis owes Rich some money
  38. */
  39. // create a tag for our new property
  40. enum vertex_first_name_t { vertex_first_name };
  41. namespace boost {
  42. BOOST_INSTALL_PROPERTY(vertex, first_name);
  43. }
  44. template <class EdgeIter, class Graph>
  45. void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G)
  46. {
  47. // Access the propety acessor type for this graph
  48. typedef typename property_map<Graph, vertex_first_name_t>
  49. ::const_type NamePA;
  50. NamePA name = get(vertex_first_name, G);
  51. typedef typename boost::property_traits<NamePA>::value_type NameType;
  52. NameType src_name, targ_name;
  53. while (first != last) {
  54. src_name = boost::get(name, source(*first,G));
  55. targ_name = boost::get(name, target(*first,G));
  56. cout << src_name << " owes "
  57. << targ_name << " some money" << endl;
  58. ++first;
  59. }
  60. }
  61. int
  62. main()
  63. {
  64. {
  65. // Create the graph, and specify that we will use std::string to
  66. // store the first name's.
  67. typedef adjacency_list<vecS, vecS, directedS,
  68. property<vertex_first_name_t, std::string> > MyGraphType;
  69. typedef pair<int,int> Pair;
  70. Pair edge_array[11] = { Pair(0,1), Pair(0,2), Pair(0,3), Pair(0,4),
  71. Pair(2,0), Pair(3,0), Pair(2,4), Pair(3,1),
  72. Pair(3,4), Pair(4,0), Pair(4,1) };
  73. MyGraphType G(5);
  74. for (int i=0; i<11; ++i)
  75. add_edge(edge_array[i].first, edge_array[i].second, G);
  76. property_map<MyGraphType, vertex_first_name_t>::type name
  77. = get(vertex_first_name, G);
  78. boost::put(name, 0, "Jeremy");
  79. boost::put(name, 1, "Rich");
  80. boost::put(name, 2, "Andrew");
  81. boost::put(name, 3, "Jeff");
  82. name[4] = "Kinis"; // you can use operator[] too
  83. who_owes_who(edges(G).first, edges(G).second, G);
  84. }
  85. cout << endl;
  86. return 0;
  87. }