ノード(ベクトルインデックス)、移動先のノード(map-> second)、およびコスト(map-> first)を格納するために使用しているものがありますvector
。maps
グラフ化に使用されます。コストがかかるノードに新しいノードを挿入できる関数を作成できません。
例:
vector[first_node].insert(make_pair(cost, second_node);
上記の方法を使用する[first_node]
と、ベクター内の場所に既存のノードがある場合、まったく新しいポイントを作成するのではなく、そのマップに追加されます。を使ってみましpush_back
たが、うまくいかないようです。
典型的なエラー:
error C2664: 'void std::vector<_Ty>::push_back(std::map<_Kty,double> &&)' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'std::map<_Kty,_Ty> &&'
for line: `edges.push_back(make_pair(second, cost));`
関数全体に関連するコード:
template <typename T>
void Graph<T>::map_edges(T first, T second, double cost) {
edges.resize(nodes.size()); //Resize the edges vector to make room for the new insertion. The new node already exists in a set of nodes called 'nodes'.
// Loop through the existing set of nodes...
for (set<T>::iterator node_it = nodes.begin();
node_it != nodes.end(); node_it++) {
//Check if the first node exists already.
if (first == *node_it) {
set<T>::iterator node_begin = nodes.begin();
set<T>::iterator node_pos = nodes.find(first);
int pos = distance(node_begin, node_pos);
//Then assign it's numerical value to the vector slot, and add the
//second and third words as a pair associated with that slot.
edges[pos].insert(make_pair(second, cost));
// edges.push_back(map<double, T>()); //This is where I would rather insert at a new location.
break;
}
}
}
エッジの定義:
vector<map<T, double> > edges;