有人可以向像我这样的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;
}
答案 0 :(得分:9)
PropertyMaps的核心是数据访问的抽象。泛型编程中出现的问题很快就是:如何获取与某个对象相关的数据?它可以存储在对象本身中,对象可以是指针,它可以在某个映射结构中位于对象之外。
你当然可以在仿函数中封装数据访问,但这很快就会变得单调乏味,你会寻找一个更窄的解决方案,在Boost中选择的是PropertyMaps。
记住这只是概念。具体实例例如是std::map
(带有一些句法自适应),一个函数返回键的成员(再次,通过一些语法自适应)。
向您的修改:make_iterator_property_map
构建iterator_property_map。第一个参数提供了一个迭代器,用于偏移计算的基础。第二个参数同样是一个property_map来进行偏移计算。这提供了一种使用vertex_descriptor
根据vector
的索引将数据写入vertex_descriptor
的方法。