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