Open In App

unordered_multiset bucket_count() function in C++ STL

Last Updated : 02 Aug, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The unordered_multiset::bucket_count() is a built-in function in C++ STL which returns the total number of buckets in the unordered_multiset 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_multiset_name.bucket_count()
Parameters: The function does not accepts 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: CPP
// C++ program to illustrate the
// unordered_multiset::bucket_count() function
#include <bits/stdc++.h>
using namespace std;

int main()
{

    // declaration
    unordered_multiset<char> sample;

    // inserts element
    sample.insert('a');
    sample.insert('b');
    sample.insert('b');
    sample.insert('b');
    sample.insert('z');

    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 << " ";
    }
    return 0;
}
Output:
The total count of buckets: 7
Bucket 0: b b b 
Bucket 1: empty
Bucket 2: empty
Bucket 3: z 
Bucket 4: empty
Bucket 5: empty
Bucket 6: a
Program 2: CPP
// C++ program to illustrate the
// unordered_multiset::bucket_count() function
#include <bits/stdc++.h>
using namespace std;

int main()
{

    // declaration
    unordered_multiset<char> sample;

    // inserts element
    sample.insert('a');
    sample.insert('b');
    sample.insert('b');
    sample.insert('b');
    sample.insert('z');

    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 << " ";
    }
    return 0;
}
Output:
The total count of buckets: 7
Bucket 0: b b b 
Bucket 1: empty
Bucket 2: empty
Bucket 3: z 
Bucket 4: empty
Bucket 5: empty
Bucket 6: a

Next Article
Practice Tags :

Similar Reads