【问题标题】:Output BGL Edge Weights输出 BGL 边缘权重
【发布时间】:2011-01-26 22:05:55
【问题描述】:

我正在尝试遍历图形的边缘并输出它们的边缘权重。不过我很困惑。我知道如何输出“边缘”,但这实际上只是一个定义边缘的 (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;
}

有什么想法吗?

谢谢, 大卫

【问题讨论】:

标签: boost boost-graph


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-29
    • 2015-04-06
    • 2011-12-18
    • 2017-09-24
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多