我在 C++ (gcc) 中有以下地图:
map<int, EdgeExtended> myMap;
其中 EdgeExtended 的定义是:
struct EdgeExtended {
int neighborNodeId;
int weight;
int arrayPointer;
bool isCrossEdge;
EdgeExtended(Edge _edge, int _arrayPointer) {
neighborNodeId = _edge.neighborNodeId;
weight = _edge.weight;
arrayPointer = arrayPointer;
isCrossEdge = _edge.isCrossEdge;
}
EdgeExtended(const EdgeExtended & _edge) {
neighborNodeId = _edge.neighborNodeId;
weight = _edge.weight;
arrayPointer = _edge.arrayPointer;
isCrossEdge = _edge.isCrossEdge;
}
EdgeExtended(int _neighborNodeId, int _weight, bool _isCrossEdge, int _arrayPointer) {
neighborNodeId = _neighborNodeId;
weight = _weight;
arrayPointer = _arrayPointer;
isCrossEdge = _isCrossEdge;
}
void setValues(int _neighborNodeId, int _weight, bool _isCrossEdge, int _arrayPointer) {
neighborNodeId = _neighborNodeId;
weight = _weight;
arrayPointer = _arrayPointer;
isCrossEdge = _isCrossEdge;
}
EdgeExtended() {
neighborNodeId = -1;
weight = -1;
arrayPointer = -1;
isCrossEdge = false;
}
};
我想这样做(简单的例子):
EdgeMap edge;
int nodeId=18;
edge=map.erase(nodeId);
a)此代码是否正确,擦除是否返回与键对应的对象?b) 如果是,当密钥不存在时,擦除返回什么?c)如果此代码错误,我如何检查是否存在键,对象映射到键,然后从映射中删除该对。请记住,性能相当关键,所以我需要最有效的方法。