有人能向像我这样的Boost初学者解释什么是Boost中的属性图吗?我在尝试使用BGL计算强连接组件时遇到了这个问题。我浏览了属性映射和图形模块的文档,但仍然不知道该怎么做。以这个代码为例:
- makeiteratorpropertymap函数在做什么
- 这个代码的含义是什么: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;
}
PropertyMaps的核心是数据访问的抽象。在泛型编程中很快出现的一个问题是:如何获取与某个对象相关的数据?它可以存储在对象本身中,对象可以是指针,也可以在某些映射结构中位于对象外部。
当然,您可以将数据访问封装在函子中,但这很快就会变得乏味,您需要寻找一个更窄的解决方案,Boost中选择的解决方案是PropertyMaps。
记住,这只是一个概念。例如,具体的实例是std::map
(具有一些语法自适应),一个返回键的成员的函数(同样具有一些语法适应性)。
编辑:make_iterator_property_map
构建迭代器属性映射。第一个参数为偏移量计算提供了一个迭代器。第二个参数是property_map,用于进行偏移量计算。这一起提供了一种基于vertex_descriptor
的索引使用vertex_descriptor
向vector
写入数据的方式。