Search code examples
c++pythondictionaryhashmaptr1

The difference between python dict and tr1::unordered_map in C++


I have a question related to understanding of how python dictionaries work.

I remember reading somewhere strings in python are immutable to allow hashing, and it is the same reason why one cannot directly use lists as keys, i.e. the lists are mutable (by supporting .append) and hence they cannot be used as dictionary keys.

I wanted to know how does implementation of unordered_map in C++ handles these cases. (since strings in C++ are mutable)


Solution

  • Keys in all C++ map/set containers are const and thus immutable (after added to the container).

    Notice that C++ containers are not specific to string keys, you can use any objects, but the constness will prevent modifications after the key is copied to the container.