473,324 Members | 2,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

map::erase

I have a map<int, obj>. When I do a map::erase(iterator), does this call the
destructor of the object? Or does it just remove that entry from the map.
Jul 23 '05 #1
5 7437
Viral Shah wrote:
I have a map<int, obj>. When I do a map::erase(iterator), does this call the
destructor of the object? Or does it just remove that entry from the map.


The entry in the map _contains_ the object. Of course while removing the
entry, it _has_to_ destroy the object.

V
Jul 23 '05 #2
Viral Shah wrote:
I have a map<int, obj>. When I do a map::erase(iterator), does this call the
destructor of the object? Or does it just remove that entry from the map.


It calls the destructor. A note: if the map is actually a map of
pointers to obj's and not actually obj's themselves, then what is
destroyed is the pointer to the obj - not the obj itself. In other
words, erase doesn't call 'delete' for you if the map is declared like
this: 'map<int, obj*>'.

Take care,

John Dibling

Jul 23 '05 #3
On 2005-06-21 21:46:53 +0100, "John Dibling" <jd******@gmail.com> said:
Viral Shah wrote:
I have a map<int, obj>. When I do a map::erase(iterator), does this call the
destructor of the object? Or does it just remove that entry from the map.


It calls the destructor. A note: if the map is actually a map of
pointers to obj's and not actually obj's themselves, then what is
destroyed is the pointer to the obj - not the obj itself. In other
words, erase doesn't call 'delete' for you if the map is declared like
this: 'map<int, obj*>'.


But it *does* call delete if your map is of the form map<int, smart_ptr<T> >
for some suitable definition of smart_ptr<T>

I know: I'm using this all the time :-)
--
JFB

Jul 23 '05 #4
verec wrote:
On 2005-06-21 21:46:53 +0100, "John Dibling" <jd******@gmail.com>
said:
Viral Shah wrote:
I have a map<int, obj>. When I do a map::erase(iterator), does this
call the destructor of the object? Or does it just remove that
entry from the map.


It calls the destructor. A note: if the map is actually a map of
pointers to obj's and not actually obj's themselves, then what is
destroyed is the pointer to the obj - not the obj itself. In other
words, erase doesn't call 'delete' for you if the map is declared
like this: 'map<int, obj*>'.


But it *does* call delete if your map is of the form map<int,
smart_ptr<T> > for some suitable definition of smart_ptr<T>

I know: I'm using this all the time :-)


But that's not the feature of the map. It's the destruction of the
'smart_ptr<T>' object what does the 'delete'ion.

V
Jul 23 '05 #5
On 2005-06-22 02:36:42 +0100, "Victor Bazarov" <v.********@comAcast.net> said:
verec wrote:
On 2005-06-21 21:46:53 +0100, "John Dibling" <jd******@gmail.com>
said:
Viral Shah wrote:
I have a map<int, obj>. When I do a map::erase(iterator), does this
call the destructor of the object? Or does it just remove that
entry from the map.

It calls the destructor. A note: if the map is actually a map of
pointers to obj's and not actually obj's themselves, then what is
destroyed is the pointer to the obj - not the obj itself. In other
words, erase doesn't call 'delete' for you if the map is declared
like this: 'map<int, obj*>'.


But it *does* call delete if your map is of the form map<int,
smart_ptr<T> > for some suitable definition of smart_ptr<T>

I know: I'm using this all the time :-)


But that's not the feature of the map. It's the destruction of the
'smart_ptr<T>' object what does the 'delete'ion.


Yep. That's the point of using something that can sort of "delegate"
the destruction to the real thing, yet also pose as, the real thing
when accessed from the map.
--
JFB

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Pieter Thysebaert | last post by:
Hello, I've got a question conerning erasing key-value pairs from a std::map while iterating over it. According to the STL docs, erasing an element in a map invalidates all iterators pointing...
17
by: Gernot Frisch | last post by:
restart: for (std::map<x,y>::iterator it = m.begin(); it!=m.end(); ++it) { if( it->second.isbad() ) { std::map<x,y>::iterator next = it; ++next; m.erase(it); it=next; // goto restart;
1
by: Marcin Kaliciński | last post by:
Hi, map::erase(iterator) and map::erase(iterator, iterator). In STL shipped with MSVC return value of these functions is iterator. In gcc 3.3 it is void. Which one is conforming? cheers,...
1
by: Milind | last post by:
Hello, I have a std::map which looks like typedef map<T1, map<T2, T3> > myMap; as a part of my code i delete a pair from the myMap if the nested map is empty. for example a sequence in my...
8
by: olanglois | last post by:
Hi, I was asking myself to following question. What is better to erase an element from a STL map: calling (option #1) size_type erase(const key_type& k) or calling (option #2)
6
by: Rémi | last post by:
Can someone tell me whether or not my assumptions are good? Just want to know if I'm going to run into trouble in how I'm deleting map entries. Although I like to think I know my way around C++,...
3
by: noone | last post by:
string operator()(const bool clean=true) { string rv; MPEGQUEUE::reverse_iterator i=thequeue.rbegin(); MPEGQUEUE::reverse_iterator t=thequeue.rend(); while (i!=thequeue.rend()) { if...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.