簡體   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