Search code examples
c++resizecontainersunordered

shrink_to_fit() for unordered containers?


I am wondering two things... Does it make any sense to resize an unordered containers in a manner similar to shrink_to_fit() once the container has been filled and you know there won't be any new items added to it? Probably this is not something you normally want to do, as the buckets are only about pointer size I guess. So... There would not be a lot of space to be won in resizing in most cases anyway.. or would there?

Secondly... what would that function There is rehash(<bucketcount>). But is there also a function similar to shrink_to_fit for unordered containers that remove all empty buckets and rehashes the container accordingly?


Solution

  • You want to look at load_factor.

    There's only

    void max_load_factor( float ml );
    

    and not min_load_factor, so you can't have what you're looking for, except by moving the elements into a new container.

    decltype(orig) shrunk(std::make_move_iterator(orig.begin()),
            std::make_move_iterator(orig.end());