繁体   English   中英

遍历const boost :: graph的边缘权重

[英]Iterating through edge weights of a const boost::graph

我需要遍历图的边缘并检查每个边缘的权重。 我没有修改边缘,因此我的函数采用了对图形的const引用。 但是,我知道获得边缘权重的唯一方法是访问属性映射,这似乎违反了const-ness。

void printEdgeWeights(const Graph& graph) {
  typedef Graph::edge_iterator EdgeIterator;
  std::pair<EdgeIterator, EdgeIterator> edges = boost::edges(graph);

  typedef boost::property_map<Graph, boost::edge_weight_t>::type WeightMap;
  // The following line will not compile:
  WeightMap weights = boost::get(boost::edge_weight_t(), graph);

  EdgeIterator edge;
  for (edge = edges.first; edge != edges.second; ++edge) {
    std::cout << boost::get(weights, *edge) << std::endl;
  }
}

所以我必须这样做:

Graph& trust_me = const_cast<Graph&>(graph);
WeightMap weights = boost::get(boost::edge_weight_t(), trust_me);

有办法避免这种情况吗?

顺便说一句,属性映射查找是否是恒定时间?

供参考,这是我对Graph的定义。

struct FeatureIndex { ... };
typedef boost::property<boost::vertex_index_t, int,
                        FeatureIndex>
        VertexProperty;
typedef boost::property<boost::edge_index_t, int,
        boost::property<boost::edge_weight_t, int> >
        EdgeProperty;
typedef boost::subgraph<
          boost::adjacency_list<boost::vecS,
                                boost::vecS,
                                boost::undirectedS,
                                VertexProperty,
                                EdgeProperty> >
        Graph;

谢谢!

为了将来参考,我找到了它。 这行不通

const boost::property_map<Graph, boost::edge_weight_t>::type

但是property_map定义了const_type

boost::property_map<Graph, boost::edge_weight_t>::const_type

get()的文档在此页上: http : //www.boost.org/doc/libs/1_51_0/libs/graph/doc/adjacency_list.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM