Open In App

bucket_count and bucket_size in unordered_map in C++

Last Updated : 03 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Unordered_map is an associated container that stores elements formed by the combination of key-value and a mapped value. The key value is used to uniquely identify the element and the mapped value is the content associated with the key. Both key and value can be of any type predefined or user-defined. 

Bucket: Internally, unordered_map is implemented using a hash table so, a bucket is a slot in the internal hash Table to which elements are assigned based on the hash value of their key. Buckets are numbered from 0 to (bucket_count-1). Hence this function returns the bucket no. where the element with key is located in unordered_map. 

Time Complexity: O(1).

Syntax: 

unordered_map.bucket(k);
// k is the key corresponds to which
// we want to know bucket number.

Return Value: The order number of the bucket corresponding to key k.

There are two more functions regarding bucket: 

1. std::bucket_count: This function is used to count the total no. of buckets in the unordered_map. No parameter is required to pass into this function. 

Time Complexity: O(1).

Syntax:  

unordered_map.bucket_count();

Return Value: The number of the bucket present in the hash table of unordered_map.

2. std::bucket_size: This function count the number of elements present in each bucket of the unordered_map. 

Time Complexity: Linear in the bucket size.

Syntax:  

unordered_map.bucket_size(i); 
// 'i' is the bucket number in which we want 
// to find no. of elements. (i < bucket_count)

Return Value: The number of elements present in bucket ‘i’.

CPP




// C++ program to demonstrate the use of std::bucket
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
    // Declaring umap to be of <string, double> type
    // key will be of string type and mapped value will
    // be of double type
    unordered_map<string, double> umap;
 
    // inserting values by using [] operator
    umap["PI"] = 3.14;
    umap["root2"] = 1.414;
    umap["log10"] = 2.302;
    umap["loge"] = 1.0;
    umap["e"] = 2.718;
 
    // Display bucket no. where key, value pair is located
    // using bucket(key)
    for (auto& x : umap) {
        cout << "(" << x.first << ", " << x.second << ")";
        cout << " is in bucket= " << umap.bucket(x.first)
             << endl;
    }
    cout << endl;
 
    // Count the no.of buckets in the unordered_map
    // using bucket_count()
    int n = umap.bucket_count();
    cout << "umap has " << n << " buckets.\n\n";
 
    // Count no. of elements in each bucket using
    // bucket_size(position)
    for (int i = 0; i < n; i++) {
        cout << "Bucket " << i << " has "
             << umap.bucket_size(i) << " elements.\n";
    }
 
    return 0;
}


Output

(loge, 1) is in bucket= 5
(e, 2.718) is in bucket= 4
(log10, 2.302) is in bucket= 4
(PI, 3.14) is in bucket= 0
(root2, 1.414) is in bucket= 3

umap has 7 buckets.

Bucket 0 has 1 elements.
Bucket 1 has 0 elements.
Bucket 2 has 0 elements.
Bucket 3 has 1 elements.
Bucket 4 has 2 elements.
Bucket 5 has 1 elements.
Bucket 6 has 0 elements.

We can also print all the elements present in each bucket of the unordered_map. 

CPP




// C++ program to print all elements present in each bucket
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Declaring umap to be of <string, double> type
    // key will be of string type and mapped value
    // will be of double type
    unordered_map<string, double> umap;
 
    // inserting values by using [] operator
    umap["PI"] = 3.14;
    umap["root2"] = 1.414;
    umap["log10"] = 2.302;
    umap["loge"] = 1.0;
    umap["e"] = 2.718;
 
    unsigned n = umap.bucket_count();
 
    // Prints elements present in each bucket
    for (unsigned i = 0; i < n; i++) {
        cout << "Bucket " << i << " contains: ";
        for (auto it = umap.begin(i); it != umap.end(i);
             it++)
            cout << "(" << it->first << ", " << it->second
                 << ") ";
        cout << "\n";
    }
    return 0;
}


Output

Bucket 0 contains: (PI, 3.14) 
Bucket 1 contains: 
Bucket 2 contains: 
Bucket 3 contains: (root2, 1.414) 
Bucket 4 contains: (e, 2.718) (log10, 2.302) 
Bucket 5 contains: (loge, 1) 
Bucket 6 contains: 

Use of bucket in std::unordered_map: There is a number of algorithms that require the objects to be hashed into some number of buckets, and then each bucket is processed. Let say, you want to find duplicates in a collection. You hash all items in the collection, then in each bucket, you compare items pairwise. A bit less trivial example is the Apriori algorithm for finding frequent itemsets.

 



Previous Article
Next Article

Similar Reads

unordered_set bucket_size() in C++ STL
The unordered_set::bucket_size() function is a built-in function in C++ STL which returns the total number of elements present in a specific bucket in an unordered_set container.The bucket is a slot in the unordered_set's internal hash table where elements are stored.Note: Buckets in unordered_set are numbered from 0 to n-1, where n is the total nu
2 min read
unordered_multiset bucket_size() function in C++ STL
The unordered_multiset::bucket_size() is a built-in function in C++ STL which returns the number of elements in the bucket which has the element val. It will always be lower than the bucket_count. The number of elements in a bucket influences the time it takes to access a particular element in the bucket Syntax: unordered_multiset_name.bucket_size(
2 min read
unordered_multimap bucket_size() function in C++ STL
The unordered_multimap::bucket_size() is a built-in function in C++ STL which returns the number of elements in the bucket n. Syntax: unordered_multimap_name.bucket_size(n) Parameters: The function accepts a parameter n which specifies the bucket number whose count is to be returned. Return Value: It returns an unsigned integral type which denotes
2 min read
unordered_set bucket_count() function in C++ STL
The unordered_set::bucket_count() method is a builtin function in C++ STL which returns the total number of buckets present in an unordered_set container. The bucket is a slot in the unordered_set's internal hash table where elements are stored. Note: Buckets in unordered_set are numbered from 0 to n-1, where n is the total number of buckets. Synta
2 min read
unordered_multiset bucket_count() function in C++ STL
The unordered_multiset::bucket_count() is a built-in function in C++ STL which returns the total number of buckets in the unordered_multiset container. A bucket is a slot in the container's internal hash table to which elements are assigned based on their hash value. Syntax: unordered_multiset_name.bucket_count() Parameters: The function does not a
2 min read
unordered_multimap bucket_count() function in C++ STL
The unordered_multimap::bucket_count() is a built-in function in C++ STL which returns the total number of buckets in the unordered_multimap container. A bucket is a slot in the container's internal hash table to which elements are assigned based on their hash value. Syntax: unordered_multimap_name.bucket_count() Parameters: The function does not a
2 min read
Traversing a Map and unordered_map in C++ STL
The maps are described as mapped associative containers for elements where each element has a key and value assigned to it. Another form of map container seen in the C++ STL is the unordered map. It is the same as map containers just that they don't store the data in sorted order. We can traverse map and unordered_map using 4 different ways which a
6 min read
map vs unordered_map in C++
Pre-requisite : std::map, std::unordered_mapWhen it comes to efficiency, there is a huge difference between maps and unordered maps. We must know the internal working of both to decide which one is to be used. Difference : | map | unordered_map --------------------------------------------------------- Ordering | increasing order | no ordering | of
2 min read
unordered_map operator= in C++ STL
The ‘=’ is an operator in C++ STL which copies (or moves) an unordered_map to another unordered_map and unordered_map::operator= is the corresponding operator function. There are three versions of this function. The first version takes reference of an unordered_map as an argument and copies it to an unordered_map. The second version performs a move
2 min read
unordered_map max_size in C++ STL
The unordered_map::max_size is a built in function in C++ STL. It returns maximum number of elements which unordered_map can hold. Maximum number of elements in any container depends upon system and library implementation. Syntax size unordered_map.max_size() Parameters: It does not accept any parameters. Return type: Unsigned integer a container c
2 min read
Practice Tags :