本文接着 http://blog.csdn.net/freemannnn/article/details/24546963 继续讨论。
桶的初始个数
在boost头文件boost\unordered\detail\util.hpp中定义了桶的初始数目:
static const std::size_t default_bucket_count = 11;
也就是说,一个unordered中,即使没有元素,桶的个数至少也是default_bucket_count。
影响桶的个数的因素
首先,桶的个数是根据需要实时变化的。在unordered中元素个数一定的情况下,如果桶的个数过少,就会导致每个桶中的元素个数过多,从而使查找效率低下;如果桶的个数过多,就会导致很多桶都是空的,从而使内存利用率低下。因此,动态的选取一个合适桶的个数很关键。
影响它的因素有:
1. X(size_type n)
Construct an empty container with at least n buckets (X is the container type).
unordered的构造函数,n直接指定了桶的个数。
2. X(InputIterator i, InputIterator j, size_type n)
Construct an empty container with at least n buckets and insert elements from the range [i, j) (X is the container type).
unordered的构造函数,n直接指定了桶的个数。
3. void reha