bucket_count and bucket_size in unordered_map in C++
Last Updated :
03 Jan, 2022
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;
}
OutputBucket 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.
Similar Reads
Unordered Map in C++ STL In C++, unordered_map is an unordered associative container that stores data in the form of unique key-value pairs. But unlike map, unordered map stores its elements using hashing. This provides average constant-time complexity O(1) for search, insert, and delete operations but the elements are not
7 min read
Different Ways to Initialize an unordered_map in C++ Initialization is the process of assigning the initial values to the std::unordered_map elements. In this article, we will learn different methods to initialize the std::unordered_map in C++.Table of ContentUsing Initializer ListBy Inserting Elements One by OneFrom Another std::unordered_mapFrom Ano
3 min read
Commonly Used Methods
Other Member Methods
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
5 min read
How to use unordered_map efficiently in C++ Pre-requisite: unordered_set,  unordered_map C++ provides std::unordered_set and std::unordered_map to be used as a hash set and hash map respectively. They perform insertion/deletion/access in constant average time. However, the worst-case complexity is O(n2).The reason is that the unordered_map
6 min read
map vs unordered_map in C++ In C++, map and unordered_map are the containers that store can store data in the form of key-value pairs, but they differ significantly in terms of underlying implementation and performance characteristics.The below table lists the primary differences between map and unordered_map container:mapunor
2 min read
How to create an unordered_map of tuples in C++? Tuple - A tuple is an object that can hold a number of elements. The elements can be of different data types. The elements of tuples are initialized as arguments in the order in which they will be accessed. Unordered Map does not contain a hash function for a tuple. So if we want to hash a tuple the
2 min read
How to create an unordered_map of user defined class in C++? unordered_map is used to implement hash tables. It stores key value pairs. For every key, a hash function is computed and value is stored at that hash entry. Hash functions for standard data types (int, char, string, ..) are predefined. How to use our own data types for implementing hash tables?unor
3 min read