What is a property map in BOOST?

2019-03-10 16:57发布

Can someone explain to a Boost beginner like me what is a property map is in Boost? I came across this when trying to use the BGL for calculating strong connected components. I went throw the documentation for the property map and graph module and still don't know what to make of it. Take this code, for example: - what is the make_iterator_property_map function doing? - and what is the meaning of this code: get(vertex_index, G) ?

#include <boost/config.hpp>
#include <vector>
#include <iostream>
#include <boost/graph/strong_components.hpp>
#include <boost/graph/adjacency_list.hpp>

int
main()
{
  using namespace boost;
  typedef adjacency_list < vecS, vecS, directedS > Graph;
  const int N = 6;
  Graph G(N);
  add_edge(0, 1, G);
  add_edge(1, 1, G);
  add_edge(1, 3, G);
  add_edge(1, 4, G);
  add_edge(3, 4, G);
  add_edge(3, 0, G);
  add_edge(4, 3, G);
  add_edge(5, 2, G);

  std::vector<int> c(N);
  int num = strong_components
    (G, make_iterator_property_map(c.begin(), get(vertex_index, G), c[0]));

  std::cout << "Total number of components: " << num << std::endl;
  std::vector < int >::iterator i;
  for (i = c.begin(); i != c.end(); ++i)
    std::cout << "Vertex " << i - c.begin()
      << " is in component " << *i << std::endl;
  return EXIT_SUCCESS;
}

1条回答
一夜七次
2楼-- · 2019-03-10 17:55

PropertyMaps at their core are an abstraction of data access. A problem that comes up very quickly in generic programming is: How do I get data associated with some object? It could be stored in the object itself, the object could be a pointer, it could be outside of the object in some mapping structure.

You can of course encapsulate data-access in a functor, but that becomes tedious very quickly and you look for a more narrow solution, the one chosen in Boost are PropertyMaps.

Remember this is just the concept. Concrete instances are for example an std::map (with some syntactic adaption), a function returning a member of the key (again, with some syntactic adaption).

Towards your edit: make_iterator_property_map builds an iterator_property_map. The first argument provides an iterator for a basis of offset calculations. The second argument is again a property_map to do the offset calculation. Together this provides a way to use an vertex_descriptor to write data to the vector based on the index of the vertex_descriptor.

查看更多
登录 后发表回答