Constantin
Constantin

Reputation: 17768

boost ptr_map replacing the value

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

Upvotes: 4

Views: 538

Answers (1)

Pseudonym
Pseudonym

Reputation: 2696

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.

Upvotes: 4

Related Questions