Hi I have int as key and object of class as value for unordered map If i do erase with int key as parameter, does it clear the object stored into map as well ? Whats time complexity ? O(1) right as it is unordered_map.... ? Is it good option to erase value one by one ? Erasing element will invalidate existing iterator of map ?
I tried sample code and it works as you said.... As soon as I erase an element using iterator, it calls destructor. Also both iterators (before and after the deleted iterator) prints correct value 2 and 4 as expected. So, now question is why to delete all at one go? Let it be erased one by one [obviously, I am not doing all erase at the end but doing erase of one element post some processing and then deleting others]. I thought that deleting all elements in one by one should be bottleneck of performance, but it should not be as it is O(1) ...right? https://code.sololearn.com/c3Y626Be6UAv/?ref=app
The erase method will indeed call the destructor of the object Time complexity is constant in average, but has worst case linear If you want to erase all the elements it's better to use the clear method Erasing an element does invalidate iterators (and references) to that element, but it does not invalidate iterators to other elements of the map