什么是BOOST中的属性映射?

时间:2022-12-27 08:02:19

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) ?

有人能向像我这样的新手解释一下什么是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;
}

1 个解决方案

#1


7  

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.

当然,您可以将数据访问封装在一个函数中,但这很快就会变得非常乏味,您需要寻找一个更窄的解决方案,Boost中选择的是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).

记住,这只是一个概念。例如,具体的实例是std::map(带有一些语法调整),一个返回键成员的函数(同样,带有一些语法调整)。

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.

对您的编辑:make_iterator_property_map构建一个iterator_property_map。第一个参数提供了一个迭代器,以作为偏移计算的基础。第二个参数是property_map,用于进行偏移计算。这提供了一种方法,可以使用vertex_descriptor来基于vertex_descriptor的索引将数据写入矢量。

#1


7  

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.

当然,您可以将数据访问封装在一个函数中,但这很快就会变得非常乏味,您需要寻找一个更窄的解决方案,Boost中选择的是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).

记住,这只是一个概念。例如,具体的实例是std::map(带有一些语法调整),一个返回键成员的函数(同样,带有一些语法调整)。

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.

对您的编辑:make_iterator_property_map构建一个iterator_property_map。第一个参数提供了一个迭代器,以作为偏移计算的基础。第二个参数是property_map,用于进行偏移计算。这提供了一种方法,可以使用vertex_descriptor来基于vertex_descriptor的索引将数据写入矢量。