2

我正在尝试遍历图形的边缘并输出它们的边缘权重。不过我很困惑。我知道如何输出“边缘”,但这实际上只是一个定义边缘的 (vertex, vertex)。那么我是否将 *edgePair.first 索引到 EdgeWeightMap 以获取从顶点 *edgePair.first 开始的边的权重?这不会编译:“不匹配运算符<<”。

#include <iostream>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>

typedef boost::property<boost::edge_weight_t, double> EdgeWeightProperty;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, EdgeWeightProperty> Graph;

int main(int,char*[])
{
  // Create a graph object
  Graph g(2);

  EdgeWeightProperty e = 5;
  add_edge(0, 1, e, g);

  boost::property_map<Graph, boost::edge_weight_t>::type EdgeWeightMap = get(boost::edge_weight_t(), g);

  typedef boost::graph_traits<Graph>::edge_iterator edge_iter;
  std::pair<edge_iter, edge_iter> edgePair;
  for(edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first)
  {
      std::cout << EdgeWeightMap[*edgePair.first] << " ";
  }

  return 0;
}

有什么想法吗?

谢谢,大卫

4

1 回答 1

4

在这段代码中,EdgeWeightProperty被声明为顶点属性而不是边属性,因此插入具有该属性的边没有意义。尝试在你的typedef中添加boost::no_propertybefore 。此外,您可能希望使用而不是因为这将适用于更多属性映射类型。EdgeWeightPropertyadjacency_listget(EdgeWeightMap, *edgePair.first)operator[]

于 2011-01-27T18:46:32.890 回答