I have the following code:
std::unordered_map<std::string, std::string> map;
map["k1"] = "v1";
auto& v1 = map["k1"];
map["k2"] = "v2";
The swap functions do not invalidate any of the iterators inside the container, but they do invalidate the iterator marking the end of the swap region.
References and pointers to either key or data stored in the container are only invalidated by erasing that element, even when the corresponding iterator is invalidated.
It looks like v1 can be safely used after inserting new values, even if re-hashing might occur during insertion.
Yes, std::unordered_map::operator doesn't invalidate references, even rehashing happens.
If an insertion occurs and results in a rehashing of the container, all iterators are invalidated. Otherwise iterators are not affected. References are not invalidated.
From the standard, $23.2.6/9 Unordered associative containers [unord.req]:
Rehashing invalidates iterators, changes ordering between elements, and changes which buckets elements appear in, but does not invalidate pointers or references to elements.