solti solti - 15 days ago 5
C++ Question

How to create unordered_map of specific size?

I know we can do this

std::unordered_map<key, value> my_map;

When I try to look at the size it says
zero
and when I try to look at bucket_count it gives
11
.

What I wanted to do was something like this
std::unordered_map<key, value> my_map(1000);


But I get error
expected identifier before numeric constant


I want to set the size of the hash table so that I won't have the same problem vector has when vector reallocates memory (copy of the values from old memory location(small) to new memory location (larger)).
Since I have large data of specific size (~100,000) that I want to hash, this motivates me to do preallocation


When I see unordered_map constructor in cppreference I unordere_map expects allocator, hash function etc. Do we really need do provide this much information to just set my hash map to specific size?

Answer