Skip to content
Related Articles

Related Articles

Improve Article

unordered_multimap bucket_size() function in C++ STL

  • Last Updated : 08 Aug, 2018
Geek Week

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:




// 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;
}
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:




// 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;
}
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
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :