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 the number of elements in the bucket with bucket number n.

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_size()
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<int, int> sample;
  
    // inserts 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 << "\nNumber of elements in Bucket " << i
             << " = " << sample.bucket_size(i);
    }
    return 0;
}

chevron_right


Output:

The total count of buckets: 7
Number of elements in Bucket 0 = 0
Number of elements in Bucket 1 = 1
Number of elements in Bucket 2 = 1
Number of elements in Bucket 3 = 2
Number of elements in Bucket 4 = 0
Number of elements in Bucket 5 = 0
Number of elements in Bucket 6 = 1

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multimap::bucket_size() 
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<char, char> sample;
  
    // inserts 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 << "\nNumber of elements in Bucket " << i
             << " = " << sample.bucket_size(i);
    }
    return 0;
}

chevron_right


Output:

The total count of buckets: 7
Number of elements in Bucket 0 = 1
Number of elements in Bucket 1 = 1
Number of elements in Bucket 2 = 1
Number of elements in Bucket 3 = 0
Number of elements in Bucket 4 = 0
Number of elements in Bucket 5 = 0
Number of elements in Bucket 6 = 2


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.