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.
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.
map::clear() clear() function is used to remove all the elements from the map container and thus leaving it's size 0.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With