c++ - boost::unordered_map is... ordered? -


I have a boost: unordered_map, but it appears in the sequence, "I'm doing you wrong". Why is this output in order? I was hoping for the built-in hashing algorithm to make this order random:

  #include & lt; Iostream & gt; # Include & lt; Boost / unordered_map.hpp & gt; Int main () {boost :: unordered_map & lt; Int, int & gt; I am; For (int i = 0; i <50; ++ i) {im.insert (std :: make_pair (i, i)); } Boost :: unordered_map & lt; Int, int & gt; :: const_iterator i; For (i = im.begin (); i! = Im.end (); ++ i) {std :: cout & lt; & Lt; I-> First & lt; & Lt; "," & Lt; & Lt; I-> Second & lt; & Lt; Std :: endl; } Return 0; }  

... gives me ...

  0, 0, 1, 2, 2 ... 47, 47 48, 48 49 , On promoting source code check:  
  inline std :: size_t hash_value (int v) {return static_cast & lt; Std :: size_t & gt; 49  

, (v); }

... who will explain it. Below are the answers at the higher levels, which I found useful.

While speaking with the intern who encourages me because I'm not a C + + man, I can offer some high-level questions that can reduce your worries:

1) What is the guarantee of the "Anord" map? Say you have an ordered map, and you want to create a map that does not guarantee order. The initial implementation could use the map respectively. Strong guarantees that there is no guarantee in terms of advertising.

2) A hash function is something that x -> int if you already have an integer, you can use the identity function. Although it can not be the most efficient in all cases, it can explain the behavior you see.

Actually, seeing this kind of behavior is not a problem.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -