C++ 为什么是对象的价值';在c+;中将其指针添加到映射后,s属性返回零+;

C++ 为什么是对象的价值';在c+;中将其指针添加到映射后,s属性返回零+;,c++,memory,pointers,map,vector,C++,Memory,Pointers,Map,Vector,所以我有这个: //sons is an attribute of the object node that is a vector<Node*> that is initialized before map<string,Node*> nodes; string node_id = "node"; string son_id = "son"; Node *node = new Node(node_id, matrix, son_id, Prim); cout <

所以我有这个:

//sons is an attribute of the object node that is a vector<Node*> that is initialized before
map<string,Node*> nodes;
string node_id = "node";
string son_id = "son";

Node *node = new Node(node_id, matrix, son_id, Prim);
cout << "Before " << node << endl;
cout << "Value of sons before map: " << node->sons[0] << endl;

nodes[node_id] = node;

cout << "After: " << nodes.find(node_id)->second << endl;
cout << "Value of sons after map: " << nodes.find(node_id)->second->sons[0];

为什么会发生这种情况?我该如何修复它?!我一直在寻找解决方案并试图解决这个问题,现在已经有4个小时了…

cout您正在使用键
node\u id
节点添加到
映射中,然后用
根id
查找它,甚至不检查它是否找到了一个,因此,通过访问
end(nodes)
,您可能会获得一些未定义的行为。您需要使用相同的键访问地图才能获得相同的对象

cout << "After: " << nodes.find(root_id)->second << endl;
cout << "Value of sons after map: " << nos.find(root_id)->second->sons[0];

此外,当您显然需要访问
节点
或映射的变量时,使用
no
nos
似乎会对变量产生一些混淆。

映射前后是什么意思?这两个输出都是在将节点添加到映射后产生的…什么是
no
nos
root\u id
?您在示例中大量混合了变量。您能否发布一个完整的、可运行的程序来演示此问题?我们无法知道您发布的代码中的哪些错误来自真实代码,哪些是复制错误。@Luchian Grigore:您怎么能说这两个输出都是在映射之后的?你真的读过代码了吗?在本例中,node_id==root_id作为:0x9dfdda8后面的行给出了正确的结果。很可能是nos vs node变量。这是一个输入错误。我有PT中的代码,只是在编写时将其翻译为EN。
cout << "After: " << nodes.find(root_id)->second << endl;
cout << "Value of sons after map: " << nos.find(root_id)->second->sons[0];