- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Unordered_map::bucket_size() Function
The C++ function unordered_map::bucket_size() function returns the number of elements present in the nth bucket. A bucket is a slot in the container's internal hash table to which elements are assigned based on the hash value of their key. Buckets have numbers ranging from 0 to (bucket_count - 1).
This function return the number of elements in each bucket which is always lessthan the count.
Syntax
Following is the Syntax of unordered_map::bucket_size() function.
unordered_map.bucket_size(n);
Parameters
n − It indicates the bucket number. that is an unsigned integral and should be lower than bucket_count.
Return value
This returns an unsigned integral that is the total number of elements in the current bucket.
Example 1
Following is the example, where we are going to use unordered_map::bucket_size().
#include <iostream> #include <unordered_map> using namespace std; int main(void){ unordered_map<char, int> um ={ {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5} }; for (int i = 0; i < um.bucket_count(); ++i) cout << "Bucket " << i << " contains "<< um.bucket_size(i) << " elements." << endl; return 0; }
Output
Following is the output of the above code −
Bucket 0 contains 0 elements. Bucket 1 contains 0 elements. Bucket 2 contains 0 elements. Bucket 3 contains 0 elements. Bucket 4 contains 0 elements. Bucket 5 contains 0 elements. Bucket 6 contains 1 elements. Bucket 7 contains 1 elements. Bucket 8 contains 1 elements. Bucket 9 contains 1 elements. Bucket 10 contains 1 elements. Bucket 11 contains 0 elements. Bucket 12 contains 0 elements.
Example 2
Consider the following example, where we are going to count the number of buckets.
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main () { std::unordered_map<std::string,std::string> map = { {"us","United States"}, {"uk","United Kingdom"}, {"fr","France"}, }; unsigned totalbuckets = map.bucket_count(); unsigned element = map.bucket_size(9); cout<<"total number of buckets"<<": "<<totalbuckets<<endl; cout << "bucket 9 has " << element << " element:\n"; return 0; }
Output
Output of the above code is as follows −
total number of buckets: 13 bucket 9 has 1 element:
Example
In the following example, we are obtaining the number of buckets and their element sizes that have at least one element in the current unordered_map.
#include <iostream> #include <unordered_map> using namespace std; int main(void) { unordered_map<char, int> um = { {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5} }; for (int i = 0; i < um.bucket_count(); ++i) { if(um.bucket_size(i)>0) { cout << "Bucket " << i << " contains "<< um.bucket_size(i) << " elements." << endl; } } return 0; }
Output
Let us compile and run the above program, this will produce the following result −
Bucket 6 contains 1 elements. Bucket 7 contains 1 elements. Bucket 8 contains 1 elements. Bucket 9 contains 1 elements. Bucket 10 contains 1 elements.