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 accept any parameter.

Return Value: It returns an unsigned integral type which denotes the total count of buckets.

Below programs illustrate the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multimap::bucket_count() 
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<int, int> sample;
  
    // inserts key and element
    sample.insert({ 10, 100 });
    sample.insert({ 10, 100 });
    sample.insert({ 20, 200 });
    sample.insert({ 30, 300 });
    sample.insert({ 15, 150 });
  
    cout << "The total count of buckets: " 
         << sample.bucket_count();
  
    // prints all element bucket wise
    for (int i = 0; i < sample.bucket_count(); i++) {
  
        cout << "\nBucket " << i << ": ";
  
        // if bucket is empty
        if (sample.bucket_size(i) == 0)
            cout << "empty";
  
        for (auto it = sample.cbegin(i); 
                  it != sample.cend(i); it++)
            cout << "{" << it->first << ", " 
                 << it->second << "}, ";
    }
    return 0;
}

chevron_right


Output:

The total count of buckets: 7
Bucket 0: empty
Bucket 1: {15, 150}, 
Bucket 2: {30, 300}, 
Bucket 3: {10, 100}, {10, 100}, 
Bucket 4: empty
Bucket 5: empty
Bucket 6: {20, 200},

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multimap::bucket_count() 
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<char, char> sample;
  
    // inserts key and element
    sample.insert({ 'a', 'b' });
    sample.insert({ 'a', 'b' });
    sample.insert({ 'b', 'c' });
    sample.insert({ 'r', 'a' });
    sample.insert({ 'c', 'b' });
  
    cout << "The total count of buckets: " 
         << sample.bucket_count();
  
    // prints all element bucket wise
    for (int i = 0; i < sample.bucket_count(); i++) {
  
        cout << "\nBucket " << i << ": ";
  
        // if bucket is empty
        if (sample.bucket_size(i) == 0)
            cout << "empty";
  
        for (auto it = sample.cbegin(i); 
                    it != sample.cend(i); it++)
            cout << "{" << it->first << ", " 
                 << it->second << "}, ";
    }
    return 0;
}

chevron_right


Output:

The total count of buckets: 7
Bucket 0: {b, c}, 
Bucket 1: {c, b}, 
Bucket 2: {r, a}, 
Bucket 3: empty
Bucket 4: empty
Bucket 5: empty
Bucket 6: {a, b}, {a, b},


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.