提升ptr_map替换值

boost ptr_map replacing the value

本文关键字:替换 map ptr 提升      更新时间:2023-10-16

给定一个键,我正在尝试替换一个值。 对于不使用指针的常规地图,我只是使用了以下调用

iter->second = object; //Where object was passed in by reference

如何使用 boost::p tr_map 实现相同的目标? 这里的概念是,我们使用derived_object替换整个类

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,因此您可以根据需要获取替换的对象。当然,如果你忽略它,它就会被自动摧毁,你永远不需要知道它在那里。