Iterating over the weights of the edges of const boost :: graph

I need to iterate around the edges of the graph and check the weight of each edge. I do not change the edges, so my function accepts a const link to the chart. However, the only way I know to get the weight of the borders is to access the property map, which apparently violates the constant.

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

So I have to do this:

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

Is there any way to avoid this?

On a side note, will property searches be constant?

For reference, here is my definition of 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;

Thank!

+5
source share
1 answer

In the future, I found it. This will not work

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

but property_map defines 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

+4

All Articles