Open In App
Related Articles

unordered_set bucket_count() function in C++ STL

Improve Article
Save Article
Like Article

The unordered_set::bucket_count() method is a builtin function in C++ STL which returns the total number of buckets present in an unordered_set container. The bucket is a slot in the unordered_set’s internal hash table where elements are stored. 

Note: Buckets in unordered_set are numbered from 0 to n-1, where n is the total number of buckets. 



Parameter: This function does not accepts any parameter. 

Return Value: This function returns the current count of buckets present in the unordered_set container. 

Below programs illustrate the unordered_set::bucket_count() function: 


// CPP program to illustrate the
// unordered_set::bucket_count() function
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
    unordered_set<int> sampleSet;
    // Inserting elements
    cout << "The sampleSet container has " << sampleSet.bucket_count()
        << " number of buckets\n\n";
    for (auto itr = sampleSet.begin(); itr != sampleSet.end(); itr++) {
        cout << "The Element " << (*itr) << " is present in the bucket: "
            << sampleSet.bucket(*itr);
        cout << endl;
    return 0;


The sampleSet container has 11 number of buckets

The Element 25 is present in the bucket: 3
The Element 5 is present in the bucket: 5
The Element 10 is present in the bucket: 10
The Element 15 is present in the bucket: 4
The Element 20 is present in the bucket: 9

Time Complexity-O(1)

Last Updated : 07 Jun, 2023
Like Article
Save Article
Similar Reads