给定一个键,我试图替换一个值。对于不使用指针的常规 map ,我只是使用了以下调用
iter->second = object; //Where object was passed in by reference
如何使用 boost::ptr_map 实现相同的效果?这里的概念是我们使用衍生对象替换整个类
iter->second = derived_object; //derived_object is a base_object pointer
最佳答案
这将解决问题:
the_map.replace(iter, derived_object);
当然,
the_map
是 iter
指向的映射。请注意,
ptr_map<K,V>::replace
返回一个 ptr_map<K,V>::auto_type
,因此您可以根据需要获取替换的对象。当然,如果你忽略它,它会自动销毁,你永远不需要知道它在那里。关于c++ - boost ptr_map 替换值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14512642/