![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- 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_multimap::bucket_count() Function
The C++ std::unordered_multimap::bucket_count() function is used returns the total number of buckets present in unordered_multimap container. 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. They have numbers ranging from 0 to bucket_count - 1.
Syntax
Following is the syntax of std::unordered_multimap::bucket_count() function.
size_type bucket_count() const noexcept;
Parameters
This function does not accepts any parameter.
Return value
This function returns the total number of bucket present in the unordered_multimap.
Example 1
In the following example, let's see the usage of unordered_multimap::bucket_count() function.
#include <iostream> #include <unordered_map> using namespace std; int main(void) { unordered_multimap<char, int> umm = { {'a', 1}, {'b', 2}, {'c', 3}, {'b', 2}, {'c', 3}, {'e', 5} }; cout << "Number of buckets = " << umm.bucket_count() << endl; return 0; }
Output
If we run the above code it will generate the following output −
Number of buckets = 7
Example 2
Consider the following example, where we are going to use the bucket_count() to get the total number of buckets and no.of items in each bucket.
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main () { unordered_multimap<string, string> UnorderMmap={ {"Aman","Akash"}, {"Gautam","Garav"}, {"Anil","Sunil"}, {"Raja","Roja"}, {"Gautam","Garav"}, {"Anil","Sunil"}, }; unsigned n = UnorderMmap.bucket_count(); cout << "map has " << n << " buckets.\n"; for (unsigned i=0; i<n; ++i) { cout << "bucket #" << i << " contains: "; for (auto it = UnorderMmap.begin(i); it!=UnorderMmap.end(i); ++it) cout << "[" << it->first << ":" << it->second << "] "; cout << "\n"; } return 0; }
Output
Following is the output of the above code −
map has 7 buckets. bucket #0 contains: bucket #1 contains: [Gautam:Garav] [Gautam:Garav] [Aman:Akash] bucket #2 contains: bucket #3 contains: bucket #4 contains: [Anil:Sunil] [Anil:Sunil] bucket #5 contains: [Raja:Roja] bucket #6 contains:
Example 3
Let's look at the following example, where we are going to use the unordered_multimap that stores the duplicate key and value, and applying the bucket_count() function to get the total no.of buckets.
#include <iostream> #include <string> #include <unordered_map> using namespace std; int main () { unordered_multimap<char, int> UnorderMmap={ {'A', 2}, {'B', 2}, {'A', 1}, {'A', 2}, {'B', 1} }; unsigned n = UnorderMmap.bucket_count(); cout << "unordered_multimap has total: " << n << " buckets.\n"; return 0; }
Output
Output of the above code is as follows −
unordered_multimap has total: 5 buckets.