Pointers c++;11作为映射键共享\u ptr值

Pointers c++;11作为映射键共享\u ptr值,pointers,c++11,share,Pointers,C++11,Share,我只需将pValue插入地图,然后从地图中获取它,它的值应该是20,但不是,为什么? typedef shared_ptr<BaseObject> PtrValue; class CodeExecuteContext{ public: map<string,PtrValue> varIdentPool; }; class BaseInteger :public BaseObject{ public: int valu

我只需将pValue插入地图,然后从地图中获取它,它的值应该是20,但不是,为什么?

typedef  shared_ptr<BaseObject> PtrValue;
class CodeExecuteContext{
     public:
     map<string,PtrValue> varIdentPool;
};  



class BaseInteger :public BaseObject{
    public:
        int value;
        BaseInteger(int val):value(val){
            enumObjType = INT;
        };
};

...

PtrValue pValue = rightNode->executeCode(context);
context.varIdentPool.insert(make_pair(leftNode.idName,pValue));

BaseInteger * baseInteger = (BaseInteger *) pValue.get();
cout << "==AssignmentASTFork==  insert [ " << leftNode.idName << " , " << baseInteger->value << " ]" <<endl;

map<string,PtrValue>::iterator it;
for (it = context.varIdentPool.begin() ; it!=context.varIdentPool.end();it++) {
    BaseInteger * baseInteger = (BaseInteger *) it->second.get();
    cout << "[key : " << it->first << ", value : " << baseInteger->value << endl;
}
typedef共享值;
类CodeExecuteContext{
公众:
地图库;
};  
类BaseInteger:公共BaseObject{
公众:
int值;
BaseInteger(int-val):值(val){
enumObjType=INT;
};
};
...
PtrValue pValue=rightNode->executeCode(上下文);
insert(make_pair(leftNode.idName,pValue));
BaseInteger*BaseInteger=(BaseInteger*)pValue.get();

coutmap::insert函数返回“pair”,其中pair中的布尔值指示元素是否已插入。如果布尔值为true,则表示已插入该布尔值。如果键已经存在,则布尔值将为false,并且尚未插入键值对

尝试“auto insertResult=context.varIdentPool.insert(make_pair(leftNode.idName,pValue));” 如果insertResult.second==false,则该键已存在

迭代器(insertResult.first)将始终指向映射中的项


使用该迭代器,您可以通过“insertResult.first->second=newValue”更改映射中的值。

map::insert函数返回“pair”,其中pair中的布尔值指示元素是否已插入。如果布尔值为true,则表示已插入该布尔值。如果键已经存在,则布尔值将为false,并且尚未插入键值对

尝试“auto insertResult=context.varIdentPool.insert(make_pair(leftNode.idName,pValue));” 如果insertResult.second==false,则该键已存在

迭代器(insertResult.first)将始终指向映射中的项


使用该迭代器,您可以通过“insertResult.first->second=newValue”更改映射中的值。

我猜,映射已经包含了这个键<代码>映射::插入
在这种情况下是不可操作的-它不会用新值替换现有值。我猜,映射已经包含此键<代码>映射::插入在这种情况下是不可操作的-它不会用新值替换现有值。它可以工作!我把同一把钥匙插入地图的某个地方。因此,如果存在相同的键,则在插入时不会替换旧值。如果要插入或更新语义,请使用
map[key]=val
而不是
insert
。它可以工作!我把同一把钥匙插入地图的某个地方。因此,如果存在相同的键,则插入时不会替换旧值。如果要插入或更新语义,请使用
map[key]=val
而不是
insert