4

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

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

1 Answer 1

4

This will do the trick:

the_map.replace(iter, derived_object);

Where, of course, the_map is the map into which iter points.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.