6

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 it expects allocator, hash function etc. Do we really need do provide this much information to just set my hash map to specific size?

solti
  • 4,339
  • 3
  • 31
  • 51
  • 1
    You might be running into the "most vexing parse" problem. Try adding extra parentheses around the `1000`. – Dan Nov 21 '16 at 18:25
  • @Dan Yes that worked ... but do you know why this `std::unordered_map my_map((100))` works and not the other `std::unordered_map my_map(100)` – solti Nov 21 '16 at 19:03
  • By the way what you said works only in `gcc version 4.9.3 (MacPorts gcc49 4.9.3_0)` then I also tried if single parenthesis work then I found out that also works .. so now I am more confused because it (both single and double parenthesis) does not work with `gcc 5.4.0 20160609` – solti Nov 21 '16 at 20:12
  • See the answers to [this question](http://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets) for an explanation. – Dan Nov 22 '16 at 10:28
  • If you use braces `{}` instead of parentheses it should work all the time. – Dan Nov 22 '16 at 10:30
  • @Dan Yes using `{}` it works for both gcc version – solti Nov 22 '16 at 16:59
  • @clcto thank you for your input – solti Nov 22 '16 at 17:31

1 Answers1

5

As with vector, you can use reserve(): http://en.cppreference.com/w/cpp/container/unordered_map/reserve

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I question whether the post envelops sufficient research – P0W Nov 21 '16 at 05:34
  • 2
    Do you have an answer for why the constructor call does not work? – clcto Nov 21 '16 at 18:19
  • 1
    @clcto: Because `vector(1000)` actually creates 1000 elements which you can modify later. What would `unordered_map(1000)` do? What keys would it use to insert 1000 elements? It simply cannot have the same semantics. – John Zwinck Nov 22 '16 at 02:45
  • 1
    @JohnZwinck One overload for the [constructor](http://en.cppreference.com/w/cpp/container/unordered_map/unordered_map) takes a `size_type bucket_count` that is the *"minimal number of buckets to use on initialization."* – clcto Nov 22 '16 at 15:46
  • @clcto I think what you are saying is only for c++14 .. were you talking about this `unordered_map() : unordered_map( size_type(/*implementation-defined*/) ) {}` ? – solti Nov 22 '16 at 16:52
  • @solti no that one is the default constructor saying the default `bucket_size` is implementation defined. The one directly above or below that, `explicit unordered_map( size_type bucket_count = /*implementation-defined*/, ...` and `explicit unordered_map( size_type bucket_count, ...`, for c++11 and c++14 respectively, but it looks like most overloads take in a `size_type bucket_count`, not just one. – clcto Nov 22 '16 at 16:56
  • @clcto so `explicit unordered_map( size_type bucket_count = /*implementation-defined*/,` takes `bucket_count ` as parameter and others `const Hash& hash = Hash(),const KeyEqual& equal = KeyEqual(),const Allocator& alloc = Allocator() ); ` are defaulted. So shouldn't `std::unordered_map my_map(1000)` if that is the case (or am I missing something here)... by the way `std::unordered_map my_map{1000}` works – solti Nov 22 '16 at 17:06
  • @solti that is my point. The constructor should work, and it did for me [here](http://ideone.com/tOxxrS). – clcto Nov 22 '16 at 17:18
  • @solti is it possible you are doing this in a class declaration? http://stackoverflow.com/questions/11490988/c-compile-time-error-expected-identifier-before-numeric-constant, http://ideone.com/QBdFaO – clcto Nov 22 '16 at 17:21
  • @clcto Yes I think you are right ... I do `class A{ public: .. private: std::unordered_map my_map(1000);};` – solti Nov 22 '16 at 17:26
  • @solti: Try `my_map{1000}` instead of `my_map(1000)`. – John Zwinck Nov 23 '16 at 03:59
  • Yes I used that and it works .. and @clcto helped me find out that this questions was already answered – solti Nov 23 '16 at 04:03