unordered_multiset bucket_size() function in C++ STL

The unordered_multiset::bucket_size() is a built-in function in C++ STL which returns the number of elements in the bucket which has the element val. It will always be lower than the bucket_count. The number of elements in a bucket influences the time it takes to access a particular element in the bucket

Syntax:

unordered_multiset_name.bucket_size(val)

Parameters: The function accepts a parameter val which specifies the element whose size of the bucket is to be returned.



Return Value: It returns an unsigned integral type which denotes the number of elements in the bucket which has the element val.

Below programs illustrate the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multiset::bucket_size() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset<int> sample;
  
    // inserts element
    sample.insert(11);
    sample.insert(11);
    sample.insert(11);
    sample.insert(12);
    sample.insert(13);
    sample.insert(13);
    sample.insert(14);
  
    for (auto it = sample.begin(); it != sample.end(); it++) {
        cout << "The bucket size in which " << *it
             << " is " << sample.bucket_size(*it) << endl;
    }
    return 0;
}

chevron_right


Output:

The bucket size in which 14 is 1
The bucket size in which 11 is 3
The bucket size in which 11 is 3
The bucket size in which 11 is 3
The bucket size in which 12 is 1
The bucket size in which 13 is 2
The bucket size in which 13 is 2

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_multiset::bucket_size() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multiset<int> sample;
  
    // inserts element
    sample.insert(1);
    sample.insert(1);
    sample.insert(1);
    sample.insert(2);
    sample.insert(3);
    sample.insert(4);
    sample.insert(3);
  
    for (auto it = sample.begin(); it != sample.end(); it++) {
        cout << "The bucket size in which " << *it
             << " is " << sample.bucket_size(*it) << endl;
    }
    return 0;
}

chevron_right


Output:

The bucket size in which 1 is 3
The bucket size in which 1 is 3
The bucket size in which 1 is 3
The bucket size in which 2 is 1
The bucket size in which 3 is 2
The bucket size in which 3 is 2
The bucket size in which 4 is 1


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.