Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::unordered_map::begin(int n) behavior

Here's the code I'm running, with g++ 4.6 and -std=c++0x

std::unordered_map<int, int> um;

um.insert(std::make_pair(42, 43));
um.insert(std::make_pair(342, 343));
um.insert(std::make_pair(142, 143));
um.insert(std::make_pair(242, 243));

for(auto e : um)
std::cout << e.first << std::endl;

This prints:

242
342
42
142

Now I can access 242 with um.begin()->first or um.begin(0)->first. 342 can be accessed using um.begin(1)->first. But um.begin(2)->first or um.begin(3)->first make the program crash. With different numbers I was able to access um.begin(2)->first. I cannot explain that behavior to myself. Do I use um.begin(int) wrong?

like image 415
TeaOverflow Avatar asked Dec 13 '22 03:12

TeaOverflow


1 Answers

You got this very confused. begin(1) is a very special construction only for the unordered containers that accesses a specific bucket in the underlying hash table structure and returns a local iterator. That has nothing to do with accessing any specific element in a sort of "random access" way, which you simply cannot do.

All you can do with an unordered container is iterate over the entire collection or find a specific element by key. The elements are not accessible in any particular order, whence the name "unordered".

You can use the local iterators to iterate over each bucket [begin(n), end(n)), but of course you have to use the same idioms that you use for any range to deal with empty containers. The total number of buckets available can be discovered with the bucket_count member function.

Note that in most situations you should expect a bucket to contain either zero or one elements. The average number of elements per bucket is available (and configurable) via the load_factor member function.

like image 109
Kerrek SB Avatar answered Dec 31 '22 02:12

Kerrek SB