我正在尝试通过图的边缘迭代并输出其边缘重量。我很困惑。我知道如何输出“边”,但实际上只是一个定义边缘的(顶点,顶点)。那么,我是否要索引 *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;
}

有什么想法吗?

谢谢大卫

有帮助吗?

解决方案

在此代码中, EdgeWeightProperty 被声明为顶点属性,而不是边缘属性,因此将边缘插入该属性是没有意义的。尝试添加 boost::no_propertyEdgeWeightProperty 在你的 adjacency_list Typedef。另外,您可能要使用 get(EdgeWeightMap, *edgePair.first) 而不是 operator[] 因为那将与更多的属性图类型一起使用。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top