In other words, is it safe to store pointers to keys from a map? Or is it possible for the map to copy and move keys during its lifetime, thus invalidating existing pointers?
The docs say: "Iterator validity: No changes." Does that mean the answer to my question is "no, they cannot be copied or moved"?
Yes -- when you insert an item into an std::map, you pass it by value, so what it contains is a copy of what you passed.
Duplicate keys are not allowed in a map : map insert « map multimap « C++ Tutorial. 23.6. 1. Insert characters into map.
a map will not throw any compile/run time error while inserting value using duplicate key. but while inserting, using the duplicate key it will not insert a new value, it will return the same exiting value only. it will not overwrite.
insert() doesn't overwrite. New! Save questions or answers and organize your favorite content. Learn more.
The std::map
container template provides a node-based container, which means that iterators and references to a container element are never invalidated until the element is erased from the map. So you can hand out element key addresses to third parties as long as the map is alive and the element remains within 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