wrote:
> gfiuni2 wrote:
>> Hi,
>>
>> Given the code below:
>>
>> #include <iostream.h>
>> #include <string>
>> #include <map>
>>
>> int main()
>> {
>> map<int, string > m;
>> string *ptr;
>>
>> m[1]="one";
>> m[2]="two";
>>
>> map<int, string >::iterator it=m.find(1);
>> ptr=&(it->second);
>>
>> m.erase(m.find(2));
>>
>> cout << *ptr << endl;
>> }
>>
>>
>>
>> Is ptr a valid pointer after erasing elements from map?
>>
>>
>> Thanks in advance,
>> Jose Luis.
>
> No, it will be invalid.
> The erase is going to destroy the string object stored in the map.
Did you notice that ptr points to the string whose key is 1 whereas the item
erased is the one whose key is 2?
> You can try this by adding a user defined object to the map and then
> calling erase. The destructor will get called when erase is called.
Best
Kai-Uwe Bux