什么是BOOST的物业地图?

Mat*_*scu 11 c++ boost boost-graph boost-property-map

有人可以向像我这样的Boost初学者解释什么是属性地图是在Boost?在尝试使用BGL计算强连接组件时,我遇到了这个问题.我去了属性地图和图形模块的文档,但仍然不知道该怎么做.以此代码为例: - make_iterator_property_map函数在做什么? - 这段代码的含义是什么: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;
}
Run Code Online (Sandbox Code Playgroud)

pmr*_*pmr 10

PropertyMaps的核心是数据访问的抽象.泛型编程中出现的问题很快就是:如何获取与某个对象相关的数据?它可以存储在对象本身中,对象可以是指针,它可以在某些映射结构中位于对象之外.

当然,你可以封装在一个仿函数数据访问,但很快就变得乏味,你找一个更窄的解决方案,在一个升压选择是属性映射.

记住这只是概念.具体实例例如是std::map(具有一些语法自适应),返回键的成员的函数(再次,具有一些语法自适应).

走向你的编辑:make_iterator_property_map构建一个iterator_property_map.第一个参数提供了一个迭代器,用于偏移计算的基础.第二个参数同样是一个property_map来进行偏移计算.这一起提供了一种使用a vertex_descriptorvector根据索引写入数据的方法vertex_descriptor.