提高ptr_map替换值 [英] boost ptr_map replacing the value

查看:55
本文介绍了提高ptr_map替换值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个键,我试图替换一个值.对于不使用指针的常规地图,我只使用了以下调用

Given a key, I am trying to replace a value. With regular maps, that do not use pointers, I simply used the following call

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

如何使用boost :: ptr_map实现相同的目标?这里的概念是,我们使用派生对象替换整个类

How do I achieve the same with boost::ptr_map? The concept here is that we replace the entire class using the 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,因此您可以根据需要获取被替换的对象.当然,如果您忽略它,它会被自动破坏,并且您永远不需要知道它在那里.

Note that ptr_map<K,V>::replace returns a ptr_map<K,V>::auto_type, so you can grab the replaced object if you want. Of course, if you ignore it, it's automagically destroyed and you need never know it was there.

这篇关于提高ptr_map替换值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆