2012-09-19 15 views
5

Bir grafiğin kenarları boyunca yinelemeli ve her kenarın ağırlığını incelemeliyim. Kenarları değiştirmeyeceğim, bu yüzden fonksiyonum bir grafiğe bir const referansı alıyor. Ancak, kenar ağırlıklarını elde etmeyi bildiğim tek yol, mülk haritasına erişim sağlamaktır, ki bu da yapıyı ihlal ediyor gibi görünüyor.Bir const yükseltmesinin kenar ağırlıkları boyunca yineleme :: graph

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; 
    } 
} 

yüzden bunu yapmak zorunda: Bunu önlemek için bir yol

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

var mı?

Bir yan notta, özellik haritası aramaları sabit zaman mı olacak?

Referans için, işte benim grafik tanımı.

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; 

Teşekkürler!

cevap

İlgili konular