我正在为 Graph 类实现一个迭代器
class iterator_node {
private:
Graph* gr_;
Node* curNode_;
public:
iterator_node() : gr_(NULL),curNode_(NULL) {}
iterator_node(Graph& ddg) : gr_(&ddg),curNode_(NULL) {}
~iterator_node() {}
}
我也有一个Graph
我应该实现insert
功能的类
class Graph {
private:
//....
map<NodeID,Node*> nodes_;
public:
pair<iterator_node,bool> insert(const NodeID nodeId,const NodeLabel nodeLabel);
}
pair<iterator_node,bool> Graph::insert(const NodeID nodeId,const NodeLabel nodeLabel)
{
DDG::iterator_node itNode(*this);
pair<Graph::iterator_node,bool> res(itNode,false);
Node* node = new Node(nodeId,false,nodeLabel);
pair<map<NodeID,Node*>::iterator,bool> nodeInsRet = nodes_.insert(pair<NodeID,Node*> (nodeId,node));
if (nodeInsRet.second == false) {
std::cout << "Node with nodeID=" << nodeId << " already exists \n";
delete node;
}
else {
res.second = true;
//!!Question!!!!
//should update curNode_; in res`s iterator (res.first)
//what is a relevan correct way to do it?
}
return res;
}