c++dictionarystdstdmapunordered

will it change the address of a existed key's value when inserting new keys?


In my code,there will be inserting or deleting in a std::map,but not change the value of a existed key. will it change the address of a existed key's value when inserting/deleting new keys ?

int main()
{

    std::map<int,int> m;
    for(int i(0);i<100000;i++){
        m[i];
        std::cout<< &m[0]<<std::endl;
    }

    return 0;
}

and the result is always the same...So it just won't have influence on old keys? By the way ,what about the unordered_map?


Solution

  • The address of existing elements do not change when other elements are inserted or removed. In other words, references to the elements are not invalidated unless that particular element is erased.

    This is true for all node based containers which include the associative containers (map, set, unordered variants, multi variants of them) and linked lists. It is not true for the array based deque, vector nor string.

    Unordered associative containers may have their iterators invalidated upon insertion in case of rehashing; this does not affect the address.