我是boost图库的新手,我正在尝试使用boost图库找到Graph中从一个顶点到另一个顶点的最短路径。我找到了一些关于如何使用前任地图的说明,但这对我来说不起作用。当我尝试调用p [some_vertex]时,我收到一条错误,指出[]运算符未定义。有人能告诉我为什么以及我做错了什么?
typedef property<edge_weight_t, double, property<edge_index_t, tElementIDVector>> tEdgeProperty;
typedef property<vertex_index_t, tElementID> VertexIDPorperty;
typedef adjacency_list<vecS, setS, directedS, VertexIDPorperty, tEdgeProperty> tGraph;
typedef tGraph::vertex_descriptor tVertex;
typedef tGraph::edge_descriptor tEdge;
vector<tVertex> p(num_vertices(graph));
vector<double> d(num_vertices(graph));
// call dijkstra algorithm from boost to find shortest path
dijkstra_shortest_paths(graph, s,
predecessor_map(&p[0]).
distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, graph))));
//find path from start to end vertex
list<tVertex> pathVertices;
tVertex current = goal;
while (current != start)
{
pathVertices.push_front(current);
current = p[current]; //this is where the error occures
}
答案 0 :(得分:1)
来自cppreference:
的std ::矢量::操作符[]
reference operator[]( size_type pos ); const_reference operator[]( size_type pos ) const;
接受 size_t
指定向量元素的位置。
但是,在您的代码中,current
的类型为tVertex
。
如果您想将operator[]
与tVertex
参数一起使用,则应覆盖它。
答案 1 :(得分:0)
我无法让你的想法发挥作用,现在也不得不做出一些其他的改变,这就是为什么我现在把我的前任地图改为:
typedef boost::property_map < tGraph, boost::vertex_index_t >::type IndexMap;
typedef boost::iterator_property_map < tVertex*, IndexMap, tVertex, tVertex& > PredecessorMap;
tVertex s = start;
IndexMap indexMap = get(vertex_index, graph);
vector<tVertex> p(num_vertices(graph));
PredecessorMap predecessorMap(&p[0], indexMap);
vector<double> d(num_vertices(graph));
// call dijkstra algorithm from boost to find shortest path
dijkstra_shortest_paths(graph, s, predecessor_map(predecessorMap).distance_map(boost::make_iterator_property_map(d.begin(), get(boost::vertex_index, graph))));
//find path from start to end vertex
list<tVertex> pathVertices;
tVertex current = goal;
while (current != start)
{
pathVertices.push_front(current);
current = predecessorMap[current];
}
这实际上现在有效:)