簡體   English   中英

什么是 BOOST 中的屬性圖?

[英]What is a property map in BOOST?

有人可以向像我這樣的 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;
}

PropertyMap 的核心是數據訪問的抽象。 泛型編程中很快出現的一個問題是:如何獲取與某個對象關聯的數據? 它可以存儲在對象本身中,對象可以是指針,也可以在某個映射結構中的對象之外。

您當然可以將數據訪問封裝在函子中,但這很快就會變得乏味,您會尋找更窄的解決方案,在 Boost 中選擇的是 PropertyMaps。

記住這只是概念。 具體實例例如是一個std::map (具有一些語法適應),一個返回鍵成員的函數(同樣,具有一些語法適應)。

make_iterator_property_map您的編輯: make_iterator_property_map構建一個iterator_property_map 第一個參數為偏移量計算提供了一個迭代器。 第二個參數再次是一個 property_map 來進行偏移量計算。 這一起提供了一種使用vertex_descriptor根據vertex_descriptor的索引將數據寫入vectorvertex_descriptor

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM