How do you delete the first element of the inner map of a map of a map?
I've tried doing something like
my_map[here].erase(my_map[here].begin())
but I'm getting unexpected results. Any suggestions or resources would be great.
How do you delete the first element of the inner map of a map of a map?
I've tried doing something like
my_map[here].erase(my_map[here].begin())
but I'm getting unexpected results. Any suggestions or resources would be great.
Does my_map[here] actually exist?
You may want to find it, i.e.
if((auto it = my_map.find(here)) != my_map.end()) {
it->erase(it->begin());
}
If my_map[here] didn't exist when you tried to access it, a new element will be created there:
http://www.cplusplus.com/reference/map/map/operator%5B%5D/
If k does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value. Notice that this always increases the container size by one, even if no mapped value is assigned to the element (the element is constructed using its default constructor).
To prevent this, you can use the find
function as I have indicated above.
find
searches for the element with the specified key. If it finds something, it will return the iterator to that element. Otherwise, it will return my_map.end()
, which is NOT the last element but a special iterator signifying the end of the structure.