Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ map erase()

Tags:

c++

I have a map in which my value is dynamically allocated. When I do erase() on an element, does this free the memory OR just removes the element from the map.

I actually need to keep the memory as is. I just need to remove the element from the map since this dynamically allocated structure is used elsewhere in the code.

like image 344
Jitesh Dani Avatar asked Jul 01 '10 18:07

Jitesh Dani


People also ask

What is map erase?

map::erase() is a built-in function in C++ STL which is used to erase element from the container. It can be used to erase keys, elements at any specified position or a given range. Parameters: The function accepts one mandatory parameter key which specifies the key to be erased in the map container.

How do I remove all elements from a map?

map::clear() clear() function is used to remove all the elements from the map container and thus leaving it's size 0.

How do I delete a map key?

To delete a key from a map, we can use Go's built-in delete function. It should be noted that when we delete a key from a map, its value will also be deleted as the key-value pair is like a single entity when it comes to maps in Go.


1 Answers

The standard containers will never destroy dynamically allocated objects that you place in them when you erase the elements. Basically, if you created it then you need to destroy it.

like image 78
Troubadour Avatar answered Oct 17 '22 09:10

Troubadour