interior_pmap_bundled.cpp 2.6 KB

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