Save pointer to map

std::map<Key,Value> mymap;
(void)mymap[Key(...)]; // create value if not there
typename std::map<Key,Value>::iterator it = mymap.find(key);
it->second.pkey = &it->first; // store a pointer to the actual key

      

It's safe? In other words, is the card allowed to copy the key during insert / erase operations, resulting in invalidation Value::pkey

?

Differences in C ++ 98 vs C ++ 11 on this?

+3


source to share


1 answer


std::map

Iterators are only invalid when erasing ( erase

or clear

). Inserting new elements into the map does not affect existing iterators. It's the same in C ++ 98 and C ++ 11.



If the iterator is still valid, it follows that the key it points to remains valid.

+4


source







All Articles