A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/unordered_set-bucket-function-in-c-stl/ below:

unordered_set bucket() function in C++ STL

unordered_set bucket() function in C++ STL

Last Updated : 05 Jun, 2023

The unordered_set::bucket() method is a builtin function in C++ STL which returns the bucket number of a specific element. That is, this function returns the bucket number where a specific element is stored in the 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. 

Syntax:

unordered_set_name.bucket(element);

Parameter: This is a mandatory parameter and specifies the value of the element whose bucket number is needed to know in the unordered_set container. 

Return Value: This function returns the bucket number of the bucket in the unordered_set container where the element with value element is stored. 

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

CPP
// C++ program to illustrate the 
// unordered_set::bucket() function 

#include <iostream> 
#include <unordered_set> 

using namespace std; 

int main() 
{ 

    unordered_set<int> sampleSet; 

    // Inserting elements 
    sampleSet.insert(5); 
    sampleSet.insert(10); 
    sampleSet.insert(15); 
    sampleSet.insert(20); 
    sampleSet.insert(25); 

    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; 
} 
Output:
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)
 



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4