我有一个vector
用于maps
存储节点(矢量索引)、它要去的节点(map->second)和成本(map->first)。它将用于绘图。我无法编写一个函数,该函数允许我插入一个新节点,该节点带有成本。
例子:
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;